Link to home
Start Free TrialLog in
Avatar of Robert Berke
Robert BerkeFlag for United States of America

asked on

Is Regex.multiline a bogus property that does not do anything ?

As far as I can tell, the Multiline property of regexp does not have any effect on anything.  Can anybody give me an example where it actually changes the behavior of regexp?

For instance, in the following program it does not matter.
Sub func2()
' extract 7 digit numbers for a string, and treat everything else as a deliminter
Dim regex As New regexp
Dim Mullti, Glog, s1, s2, s3, msg, match As Object
Debug.Print "\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"
For Each multi In Array(True, False)

Debug.Print "Multiline is " & multi

For Each Glob In Array(True, False)
regex.MultiLine = Mullti '  this two values do not affect the outcome of this test
regex.Global = Glob ' this two values do not affect the outcome of this test
s1 = " 1234567" & vbLf & "567 2072083 "  ' vbLf DOES NOT match to .*
s2 = " 1234567" & vbCr & "567 2072083 "  ' vbcr DOES match to .*  regardless of the multiline value.
s3 = " 1234567" & vbCrLf & "567 2072083 "  ' vbcr DOES match, but the lf DOES NOT match to .*
regex.pattern = "(\b|\D)\d{7,7}(?=\b|\D)" ' use [\s\S]+ instead of .+  and if you REALLY want to match anything
msg = ""
If Glob Then msg = msg & "Global "
If Not Glob Then msg = msg & "Not Global "

Set match = regex.Execute(s1) '
msg = match.count & "-" & msg
Set match = regex.Execute(s2) '
msg = match.count & "-" & msg
Set match = regex.Execute(s3) '
msg = match.count & "-" & msg
Debug.Print msg
'Stop
Next
Next
Debug.Print "///////////////////////////////"
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Robert Berke

ASKER

Thanks, that helps a lot.
but, it looks to me like the following is more precise.

With Multiline off . matches any character except vbCr and vbLf
With Multiline on  . matches any character except vbLf

The following snippet illustrates my point.  Can anybody think of other exceptions besides vbLf?
reg.Global = True
reg.MultiLine = True
reg.pattern = "."
s = String(1, "a") & String(2, vbCr) & String(20, vbLf)
MsgBox reg.Execute(s).count

Open in new window

Aha,  I now remember that VBScript does not have an option to make the dot match line break characters.

If I truly truly truly need to match "any character" I must use [\s\S] instead of a period.  And that works with  Multiline on or off.
The following snippet illustrates my point.  Can anybody think of other exceptions besides vbLf?
See my comment.
To my knowledge, everything else remain unaffected.
With Multiline off . matches any character except vbCr and vbLF
With Multiline on  . matches any character except vbLf
Regardless of Multiline, [\s\S] matches any character including vbCr and vbLf

With Multiline off ^ matches the beginning position of the string
With Multiline on  ^ matches the beginning position of a line

With Multiline off $ matches the ending position of the string
With Multiline on  $ matches the ending position of a line