Jos*_*osh 0 .net c# regex non-greedy regex-greedy
这是我的意见:
xxx999xxx888xxx777xxx666yyy
xxx222xxx333xxx444xxx555yyy
这是表达式:
xxx.*xxx(?<matchString>(.(?!xxx.*xxx))*?)xxx.*yyy
Run Code Online (Sandbox Code Playgroud)
它返回444.
我希望它能同时返回444和777,但我不能随便找到它.
我有!排除,以便它只匹配左侧的最里面(当我只搜索一个结果时,这很有效,大部分时间都是这样).但是,我感觉这与它在此实例中跳过第一个结果的原因有关.我不知道从哪里开始.
我一直在这里测试: http: //regexlib.com/RETester.aspx(启用"SingleLine"和"Explicit Capture")
任何意见,将不胜感激!
首先,我想解释为什么您当前的解决方案不起作用:
由于您已启用SingleLine选项,.*匹配999xxx888xxx777xxx666yyy\nxxx222xxx333,括号中的模式匹配444,以及其他正则表达式匹配xxx555yyy.
所以要匹配两者777,444你可以禁用SingleLine选项或使用这样的东西:
xxx\d+xxx\d+xxx(?<matchString>\d+)xxx\d+yyy
Run Code Online (Sandbox Code Playgroud)