Regular expression - how to find a match in a match?

I am trying to do the following using a VBs Regular Expression object, but could not find an easy way to do this. Is there anyone who could offer some suggestions?

For example, I have the line "12 <56> 89", I would like to get the line inside "<>", which in this case should be "56". What I'm doing now, I'm trying to find the expression "<\ d +>" that will return <56>. Then I try to find the expression "\ d +" from the result of the first match, which will return 56.

I don’t like it because it has to call this function twice. I am wondering if it is possible to get a string inside "<>" using only one regular expression? Thanks!

Thanks Allen

+6
regex vbscript
source share
2 answers

Use the expression "<(\ d +)>"

Then you can access all matches as a collection. If you set RegEx.Global = True , your regular expression can match more than once. The first match is found in var (0), the second is in var (1). Submark groups are in var (0) .SubMatches (0), etc. If you do this only once, you can select one line:

 Dim RegEx : Set RegEx = New RegExp RegEx.Pattern = "<(\d+)>" Dim strTemp : strTemp = "12<56>89" WScript.Echo RegEx.Execute(strTemp)(0).SubMatches(0) 

Check your regular expressions here: http://www.regular-expressions.info/vbscriptexample.html

+8
source share

Use the expression <(\ d +)>. Perform regex using

 Set matches = expr.Execute(text) If matches.Count > 0 Then result = matches(0).Submatches(0) End If 

The Submatches collection contains strings corresponding to the parenthesis groups in the expression.

+3
source share

All Articles