RegEx and \n codes

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

RegEx and \n codes

Post by Bob Hansen » Tue Apr 20, 2004 10:19 pm

I am having trouble getting a regular expression for a return code (\n) to be accepted.

For testing, I am trying to change from the single line:
One Two Three

into three lines:
One
Two
Three

Here is the test script., with five test lines
VBSTART
Function ReplaceTest(patrn, replStr)
Dim regEx, str1 ' Create variables.
str1 = "One Two Three"
Set regEx = New RegExp ' Create regular expression.
regEx.Pattern = patrn ' Set pattern.
regEx.IgnoreCase = True ' Make case insensitive. Default=False
ReplaceTest = regEx.Replace(str1, replStr) ' Make replacement.
End Function
VBEND

//Test1: Just reverses two of the words. This is sample from MSDN library! Actual result is No Good.
VBEval>ReplaceTest("(\S+)(\s+)(\S+)","$3$2$1"),string2
MessageModal>First two words were reversed.%CRLF%"One Two Three" becomes %string2%.

//Test2: Uses normal "\n" for the return code. -FAILS
VBEval>ReplaceTest("(\S+)(\s+)(\S+)","$1\n$2\n$3"),string2
MessageModal>Three words were placed on separate lines.%CRLF%"One Two Three" becomes %string2%.

Test3: Uses variable Rtn for "\n". -FAILS
Let>Rtn=\n
VBEval>ReplaceTest("(\S+)(\s+)(\S+)","$1%Rtn%$2%Rtn%$3"),string2
MessageModal>Three words were placed on separate lines.%CRLF%"One Two Three" becomes %string2%.

//Test4: Uses "\x0a" vs. "\n" for the return code. -FAILS
VBEval>ReplaceTest("(\S+)(\s+)(\S+)","$1\x0a$2\x0a$3"),string2
MessageModal>Three words were placed on separate lines.%CRLF%"One Two Three" becomes %string2%.

Test5: Uses "%CRLF%" for the return code. -FAILS
VBEval>ReplaceTest("(\S+)(\s+)(\S+)","$1%CRLF%$2%CRLF%$3"),string2
MessageModal>Three words were placed on separate lines.%CRLF%"One Two Three" becomes %string2%.
So I guess the basic question is how do I pass "\n" to the VB for EVAL?

My actual RegEx is much more complex, requiring multiple instances of "\n" in the replacement string.
================================
I do have a second question about why the sample word reversals do no work. After doing some digging, I am thinking that I need to be using the Exec method in order to properly pick up the submatche collection values?
Why does the sample of $3$2$1 give Two One Three vs. Three Two One?
I am not used to the VB syntax for RegEx. Sample uses $1, $2, and refers to Jscript, I am used to \1, \2, etc. but see no reference to it in MSDN library. What am I missing here?
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Wed Apr 21, 2004 12:07 am

OK, it looks like I have solved my own problem.
The sensible solution is to use "vbCRLF" vs. "\n"
VBSTART
Function ReplaceTest(patrn, replStr)
Dim regEx, str1 ' Create variables.
str1 = "One Two Three"
Set regEx = New RegExp ' Create regular expression.
regEx.Pattern = patrn ' Set pattern.
regEx.IgnoreCase = True ' Make case insensitive. Default=False
ReplaceTest = regEx.Replace(str1, replStr) ' Make replacement.
End Function
VBEND

//Test6: Uses literal vbCRLF
VBEval>ReplaceTest("(\S+)(\s+)(\S+)","$1"&vbCRLF&"$3"&vbCRLF&"$2"),string2
MessageModal>vbCRLF=Three words were placed on separate lines.%CRLF%"One Two Three" becomes %string2%.

//Test7: Uses variable for vbCRLF
Let>Rtn3=&vbCRLF&
VBEval>ReplaceTest("(\S+)(\s+)(\S+)","$1"%Rtn3%"$3"%Rtn3%"$2"),string2
MessageModal>Rtn3(vbCRLF)=Three words were placed on separate lines.%CRLF%"One Two Three" becomes %string2%.
================================

So now I am still trying to figure out the use of $1, $2, $3. they do not seem to follow the grouped expressions in the string being processed.
Original string: "One Two Three"
RegEx string: "(\S+)(\s+)(\S+)"
Expected: $1=One, $2=Two, $3=Three
But get: $1=One, $2=Three, $3=Two

All suggestions are welcome!
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Wed Apr 21, 2004 1:18 am

Too slow out there! I solved my own problem again.
The problem was the old DUBKAM (DUmmy Between Keyboard And Monitor). :oops:

I was not looking at the contents of the grouped expressions. Not looking closely, I thought it was the same as (.*), but it was actually looking at blank space, non blank space, and blank space again.

These tests show that you can use vbCRLF and vbNewline as literals or as variables in place of "\n" for the EndOfLine character. Is also shows that a literal "space" character or "\s" for whitespace can be used to help isolate the grouped expressions One Two Three in their normal order.
VBSTART
Function ReplaceTest(patrn, replStr)
Dim regEx, str1 ' Create variables.
str1 = "One Two Three"
Set regEx = New RegExp ' Create regular expression.
regEx.Pattern = patrn ' Set pattern.
regEx.IgnoreCase = True ' Make case insensitive. Default=False
ReplaceTest = regEx.Replace(str1, replStr) ' Make replacement.
End Function
VBEND

//Test8: Uses literal vbCRLF and space between groups
VBEval>ReplaceTest("(.*) (.*) (.*)", "$1"&vbCRLF&"$2"&vbCRLF&"$3"),string2
MessageModal>Three words are placed on separate lines.%CRLF%"One Two Three" becomes %string2%.

//Test9: Uses variable for vbNewline and "\s" between groups
Let>Rtn3=&vbNewline&
VBEval>ReplaceTest("(.*)\s(.*)\s(.*)", "$1"%Rtn3%"$2"%Rtn3%"$3"),string2
MessageModal>Three words are placed on separate lines.%CRLF%"One Two Three" becomes %string2%.
So I now have a sensible solution for using RegEx in Macro Scheduler. Whew! Thanks for your patience and support.
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts