faridsalameh
asked on
Regular Expressions
hi, I'm trying to set up a regex pattern to detect occurrences of
/* ...any number of characters including CR & LF encased like this.... */
in a string.
I'm using the code below, expecting the FOR loop to return
/* my first comment */
first time around, and
/* my second one */
the next time around. Instead, what I get is
/* my first comment */ and this is /* my second one */
what am I doing wrong?
Is there another way of doing what I need to do?
here is the code I'm using...
Dim re As New RegExp
Dim t As String
Dim thisMatch As Match
Dim mtchs As MatchCollection
t = "This is /* my first comment */ and this is /* my second one */ and this is the rest"
re.Pattern = "/\*.*\*/"
re.Global = True
re.MultiLine = True
Set mtchs = re.Execute(t)
For Each m In mtchs
Set thisMatch = m
MsgBox thisMatch.value
Next
/* ...any number of characters including CR & LF encased like this.... */
in a string.
I'm using the code below, expecting the FOR loop to return
/* my first comment */
first time around, and
/* my second one */
the next time around. Instead, what I get is
/* my first comment */ and this is /* my second one */
what am I doing wrong?
Is there another way of doing what I need to do?
here is the code I'm using...
Dim re As New RegExp
Dim t As String
Dim thisMatch As Match
Dim mtchs As MatchCollection
t = "This is /* my first comment */ and this is /* my second one */ and this is the rest"
re.Pattern = "/\*.*\*/"
re.Global = True
re.MultiLine = True
Set mtchs = re.Execute(t)
For Each m In mtchs
Set thisMatch = m
MsgBox thisMatch.value
Next
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Good point, Anthony, I always forget about that!
ASKER
Anthony, this is perfect..thanks for your help. seems I need to better understand the subtleties of greedy and non-greedy operators.
Thanks also to Matt for the suggestion but Anthony's suggestion fits best.
Thanks also to Matt for the suggestion but Anthony's suggestion fits best.
ASKER
Just one final point on this...how can I make it detect cases where there are CR & LF characters in the text? For example:
/* this is line 1
and this is line 2 */
The dot implies all characters except line terminators, so the solution suggested by Anthony works fine as long as the /* and */ are on the same line. Is there a way of indicating all characters, including CR & LF?
thanks.
/* this is line 1
and this is line 2 */
The dot implies all characters except line terminators, so the solution suggested by Anthony works fine as long as the /* and */ are on the same line. Is there a way of indicating all characters, including CR & LF?
thanks.
If you change your pattern to "/\*(.|\n)*?\*/" that should take care of that issue for you (and the linefeed will be part of your match, as it is in the text)
Another way of doing this would be to add a line feed after the */, and ending the pattern with a newline, like:
t = Replace(t, "*/", "*/" & Chr(10))
re.Pattern = "/\*.*\*/\n"
then just reverse the replace you did earlier when checking the value:
MsgBox Replace(thisMatch.Value, "*/" & Chr(10), "*/")
Another way would be to still use the replace above, but then change the pattern to:
re.Pattern = "(/\*.*\*/)\n"
Then just return the first submatch, like:
MsgBox thisMatch.SubMatches(0)
Matt