I have a .net regex which I am testing using Windows Powershell. The output is as follows:
> [System.Text.RegularExpressions.Regex]::Match("aaa aaa bbb", "aaa.*?bbb")
Groups : {aaa aaa bbb}
Success : True
Captures : {aaa aaa bbb}
Index : 0
Length : 11
Value : aaa aaa bbb
My expectation was that using the ?
quantifier would cause the match to be aaa bbb
, as the second group of a's is sufficient to satisfy the expression. Is my understanding of non-greedy quantifiers flawed, or am I testing incorrectly?
Note: this is plainly not the same problem as Regular Expression nongreedy is greedy
aaa((?!aaa).)*bbb
, which is more or less what you said, except that your answer has the added details of making the subexpression non-capturing and also tests for bbb in the negative lookahead. Nice answer. – Tadtada