?
Solved

Regular Expressions

Posted on 2005-04-13
6
Medium Priority
?
235 Views
Last Modified: 2010-05-02
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
Comment
Question by:faridsalameh
  • 3
  • 2
6 Comments
 
LVL 35

Expert Comment

by:mvidas
ID: 13773972
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
 
LVL 8

Accepted Solution

by:
anthonywjones66 earned 500 total points
ID: 13774262
How about:-

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

That makes the internal of the expression non-greedy

Anthony.
0
 
LVL 35

Expert Comment

by:mvidas
ID: 13774324
Good point, Anthony, I always forget about that!
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:faridsalameh
ID: 13774396
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
 

Author Comment

by:faridsalameh
ID: 13774820
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
 
LVL 35

Expert Comment

by:mvidas
ID: 13775472
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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month17 days, 2 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question