开发者

vba positive lookahead is too greedy

I'm using Access VBA to parse a string with regex. Here's my regex function:

Function regexSearch(pattern As String, source As String) As String

Dim re As RegExp
Dim matches As MatchCollection
Dim match As match


Set re = New RegExp
re.IgnoreCase = True

re.pattern = pattern
Set matches = re.Execute(source)


    If matches.Count > 0 Then
        regexSearch = matches(0).Value
    Else
        regexSearch = ""
    End If


End Function

When I test it with开发者_StackOverflow社区:

regexSearch("^.+(?=[ _-]+mp)", "153 - MP 13.61 to MP 17.65")

I'm expecting to get:

153

because the only characters between this and the first instance of 'MP' are the ones in the class specified in the lookahead.

but my actual return value is:

153 - MP 13.61 to

Why is it capturing up to the second 'MP'?


Because .+ is greedy by default. The .+ gobbles up every character until it encounters a line break char, or the end-of-input. When that happens, it backtracks to the last MP (the second one in your case).

What you want is to match ungreedy. This can be done by placing a ? after .+:

regexSearch("^.+?(?=[ _-]+MP)", "153 - MP 13.61 to MP 17.65")
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜