9

applying the below regex on below email body:

(pls[a-zA-Z0-9 .*-]*) \(([A-Z 0-9]*)\)

email body:

pls18244a.lam64*fra-pth (PI000581) 
pls18856a.10ge*fra-pth (PI0005AW) 
pls25040a.10ge*fra-pth (IIE0004WK) 
pls27477a.10ge*fra-pth (WL050814) 
pls22099a.stm4*par-pth (PI0005TE)

returns 5 match, with two groups. what is the VBA script to get groups in each match using using incremental variable to copy each match groups in excel row?

1
  • 1
    match objects have a submatches collection, so you'd loop over that for each match. Commented Jun 7, 2017 at 3:47

1 Answer 1

21

Not making any changes to your regular expression pattern. Using the following way, you can iterate through the groups of each match:

str="pls18244a.lam64*fra-pth (PI000581)pls18856a.10ge*fra-pth (PI0005AW)pls25040a.10ge*fra-pth (IIE0004WK)pls27477a.10ge*fra-pth (WL050814)pls22099a.stm4*par-pth (PI0005TE)"
Set objReg = New RegExp
objReg.IgnoreCase=False
objReg.Global=True
objReg.Pattern = "(pls[a-zA-Z0-9 .*-]*) \(([A-Z 0-9]*)\)"
Set objMatches = objReg.Execute(str)
For Each match In objMatches             'The variable match will contain the full match
    a= match.Submatches.Count            'total number of groups in the full match  
    For i=0 To a-1
        MsgBox match.Submatches.Item(i)  'display each group
    Next
Next
Set objReg = Nothing
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, (for each)sentence should be contains matches instead of match.. and i need to get 1stmatch.1stgroup and 1stmatch.2ndgroup and so on .. in separated fields?
please could any one assist on this?
I didn't get what you are saying? You have the whole working code now. It is displaying both the groups of each match. Do you want to display the whole match?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.