Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
566 views
in Technique[技术] by (71.8m points)

regex - Regular Expression - How to find a match within a match?

I've been trying to do the following using VBs Regular Expression object but could not find an easy way to do it. Is there anyone who could provide some suggestions?

For example, I have a string "12<56>89", I would like to get the string inside the "<>" which should be "56" in this case. What I'm currently doing is I try to find the expression "<d+>" which 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 this way because it needs to call the function twice. I'm wondering if it's possible to get the string inside the "<>" using just one regular expression? Thank you!

Thanks, Allen

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use the expression "<(d+)>"

You can then access all matches as a collection. Your regex can match more than once if you set RegEx.Global = True. The first match is found in var(0), second at var(1). Submatch groups are found at var(0).SubMatches(0), etc. If you're only doing it once, you can one line it:

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...