为什么这个正则表达式只返回一个匹配项? [英] why is this regular expression returning only one match?

查看:98
本文介绍了为什么这个正则表达式只返回一个匹配项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的输入内容:

xxx999xxx888xxx777xxx666yyy

xxx999xxx888xxx777xxx666yyy

xxx222xxx333xxx444xxx555yyy

xxx222xxx333xxx444xxx555yyy

这是表达式:

xxx.*xxx(?<matchString>(.(?!xxx.*xxx))*?)xxx.*yyy

正在返回 444 .

我希望它同时返回444和777,但是我什么也收不到.

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.

我一直在这里进行测试: http://regexlib.com/RETester.aspx (带有"SingleLine"和"Explicit Capture" ;已启用)

I've been testing here: http://regexlib.com/RETester.aspx (with "SingleLine" and "Explicit Capture" enabled)

任何建议将不胜感激!

推荐答案

首先,我想解释一下为什么当前的解决方案不起作用:

First of all I want to explain why your current solution doesn't work:

自启用SingleLine选项以来,.*匹配999xxx888xxx777xxx666yyy\nxxx222xxx333,括号中的模式匹配444,其余Regex匹配xxx555yyy.

Since you've enabled SingleLine option, .* matches 999xxx888xxx777xxx666yyy\nxxx222xxx333, the pattern in parenthesis matches 444, and the rest of your Regex matches xxx555yyy.

因此,要同时匹配777444,您可以禁用SingleLine选项或使用类似以下内容的方法:

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

这篇关于为什么这个正则表达式只返回一个匹配项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆