why is this regular expression returning only one match?

不羁岁月 提交于 2019-12-12 05:13:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!