sebastian87
sebastian87

Reputation: 484

RegEx Pattern in VB for Multiline Matches

I already made it to get the information in single line. I have a list of information like:

1 1 838028476391 4 23 36 P 1/820-01 *

2 1 838028476490 4 23 36 P 1/820-17 *

3 1 838028474271 4 23 36 P 1/820-21 *

4 1 838028476292 4 23 36 P 1/820-21 *

5 1 838028474263 4 23 36 P 1/820-23 *

6 1 838028473802 4 23 36 P 1/820-21 *

And I need the 12 digits numbers from every line. I tried this code:

 Dim re As String
  Dim re18 As String
  re18 = "(\d{12})"
  Dim r3 As New RegExp
  r3.Pattern = re18
  r3.IgnoreCase = True
  r3.MultiLine = True


 If r3.Test(Body) Then
    Dim m3 As MatchCollection
    Set m3 = r3.Execute(Body)
    If m3.Item(0).SubMatches.Count > 0 Then
        Dim number
        For j = 1 To m3.Count
            Set number = m3.Item(j - 1)
            MsgBox ("Number: " + number)
        Next
    End If
  End If

I only get the first match - even if I debug the makro and view m3 in the watch - there is only 1 match. I also tried to use the quantifiers * or + after \d{12}

How do I get this RegEx working?

And regarding RegEx I have another question: If I want to match something AFTER a special word i would put the word in the pattern at the beginning and behind that the numbers or whatever I want. If I execute this regex - do I get the information or match INCLUDING the word I put at the beginning of my pattern?! Like: "BUS \d{12}" and I only want the numbers as a result but know that BUS stands before the numbers...

Upvotes: 0

Views: 2242

Answers (1)

Alan Moore
Alan Moore

Reputation: 75222

You need to use the Global option, not Multiline. Multiline changes the behavior the anchors (^ and $) so they match the beginning and end of each line, not just the beginning and end of the whole text. Global is the option that tells it to find all the matches, not just the first one.

You probably don't need to use the SubMatches property either. Your regex has only the one capturing group, which captures the whole match. That means m3.SubMatches will only contain one Item, Item(0), and it will be exactly the same as m3.Item(0). (Notice that the index of the first group is 0, not 1 as you would expect from working with other regex tools.)

Your second question is where the SubMatches property comes in. If you wanted to find every 12-digit number that follows the word "BUS" you would use a regex like this:

BUS\s*(\d{12})

...and you would retrieve the number from each match like this:

Set m3 = r3.Execute(Body)
For Each myMatch in m3
  MsgBox("Number: " + m3.SubMatches(0).Value)
Next

See this page for more info.

Upvotes: 1

Related Questions