开发者

Does the greediness of a regex matter after all need matches have been used?

The title pretty much says it all.

I have开发者_开发知识库 a regex that I need to match the names of virtual machines from an array.

The regex looks like this:

/^(?<id> \d+)\s+(?<name> .+?)\s+\[.+\]/mx

After the last capture group is matched I have no need for the left overs other than using them to stop the match at the correct place so that all characters in the capture group are correctly matched. Does it matter how greedy the left overs are if they are not being used?

Here is an example of the string I am matching, this is before the match.

432    TEST Box åäö!"''*#   [Store] TEST Box +w6XDpMO2IQ-_''_+Iw/TEST Box +w6XDpMO2IQ-_''_+Iw.vmx   slesGuest         vmx-04

Here is an example if the string I am matching, this is after the match.

432    TEST Box åäö!"''*#

Like I ask above, if I only need the first 2 capture groups does it matter how greedy the uncaptured part at the end is?


There would be no difference between \s+ and \s+? as long as the preceding quantifier .+? remains lazy; it will always match at least one space and expand as needed until the following [.

I first said that there might be a difference between \[.+\] and \[.+?\] if more than one pair of data items can occur on the same line. The former would match too much in that case. But I just noticed that you've anchored your regex to the start of the line. So no, in that case, it doesn't matter either.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜