Regular Expressions

Posted on 2005-04-13
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
Question by:faridsalameh
    LVL 35

    Expert Comment


    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)

    LVL 8

    Accepted Solution

    How about:-

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

    That makes the internal of the expression non-greedy

    LVL 35

    Expert Comment

    Good point, Anthony, I always forget about that!

    Author Comment

    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.

    Author Comment

    Just one final point on 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?

    LVL 35

    Expert Comment

    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)

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
    I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
    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…

    745 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now