Advertisement

12.08.2003 at 04:44PM PST, ID: 20820143
[x]
Attachment Details

Regular Expression approach to alter part of an Excel Formula

Asked by brettdj in Perl Programming Language

Tags: case, excel, formula

Main question:

I'm using this RegExp pattern to match Excel Cell References

Please note that I'm using VBscript

([$]{0,1}[A-Z]{1,2}[$]{0,1}\d{1,5})

it finds the formulas properly but I want to extract a nth element and convert it to absolute referencing and then put it back in the same string

ie
=A100+A100+B$10
I'd like to be able to replace the second match (A100) with $A$100 in the same string.
=A100+$A$100+B$10
Would appreciate any pointers on this


Subquestion: The approach I tried using Replace is below and it led to another query:

I found this code below at  http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/vsmthreplace.asp and I thought that it gave me a good start - but
when I tried to swap the 4th 5th and 6th matches (ie "brown fox") I couldn't get it to work

Function ReplaceTest(patrn, replStr)
  Dim regEx, str1               ' Create variables.
  str1 = "The quick brown fox jumped over the lazy dog."
  Set regEx = New RegExp            ' Create regular expression.
  regEx.Pattern = patrn            ' Set pattern.
  regEx.IgnoreCase = True            ' Make case insensitive.
  ReplaceTest = regEx.Replace(str1, replStr)   ' Make replacement.
End Function

In addition, the Replace method can replace subexpressions in the pattern. The following call to the function shown in the previous example swaps the first pair of words in the original string:

MsgBox(ReplaceTest("(\S+)(\s+)(\S+)", "$3$2$1"))   ' Swap first pair of words

How would I swap the order of "brown" and "fox"?

I tried
MsgBox(ReplaceTest("(\S+)(\s+)(\S+)", "$6$5$4"))  
and it gave me

"$6$5$4 brown fox jumped over the lazy dog."

Cheers

DaveStart Free Trial
[+][-]12.08.2003 at 10:21PM PST, ID: 9902154

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]12.08.2003 at 11:06PM PST, ID: 9902320

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]12.09.2003 at 08:31AM PST, ID: 9905233

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: Perl Programming Language
Tags: case, excel, formula
Sign Up Now!
Solution Provided By: PurplePerls
Participating Experts: 2
Solution Grade: A
 
 
[+][-]12.09.2003 at 02:14PM PST, ID: 9908076

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]12.09.2003 at 11:07PM PST, ID: 9910300

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12.16.2003 at 01:59AM PST, ID: 9948114

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]12.16.2003 at 02:21AM PST, ID: 9948164

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12.16.2003 at 01:48PM PST, ID: 9952503

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]12.16.2003 at 03:26PM PST, ID: 9953156

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32