问题
Here is my input:
xxx999xxx888xxx777xxx666yyy
xxx222xxx333xxx444xxx555yyy
This is the expression:
xxx.*xxx(?<matchString>(.(?!xxx.*xxx))*?)xxx.*yyy
It's returning 444.
I'd like it to return both 444 and 777, but I can't get anywhere with this.
I have the ! exclusion so that it matches only the innermost on the left side (which works great when I am searching for only one result, which is most of the time). However, I have a feeling that that is related to why it is skipping the first result in this instance. I'm not sure where to go from here.
I've been testing here: http://regexlib.com/RETester.aspx (with "SingleLine" and "Explicit Capture" enabled)
Any advice would be appreciated!
回答1:
First of all I want to explain why your current solution doesn't work:
Since you've enabled SingleLine
option, .*
matches 999xxx888xxx777xxx666yyy\nxxx222xxx333
, the pattern in parenthesis matches 444
, and the rest of your Regex matches xxx555yyy
.
So to match both 777
and 444
you can either disable SingleLine
option or use something like this:
xxx\d+xxx\d+xxx(?<matchString>\d+)xxx\d+yyy
来源:https://stackoverflow.com/questions/12923782/why-is-this-regular-expression-returning-only-one-match