• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 238
  • Last Modified:

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
0
faridsalameh
Asked:
faridsalameh
  • 3
  • 2
1 Solution
 
mvidasCommented:
Hello,

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
0
 
anthonywjones66Commented:
How about:-

re.Pattern = "/\*.*?\*/"

That makes the internal of the expression non-greedy

Anthony.
0
 
mvidasCommented:
Good point, Anthony, I always forget about that!
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
faridsalamehAuthor Commented:
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.
0
 
faridsalamehAuthor Commented:
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.
0
 
mvidasCommented:
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)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now