简体   繁体   English

正则表达式非贪婪匹配

[英]Regex non-greedy match

If I have the following simplified string: 如果我有以下简化的字符串:

        string331-itemR-icon253,string131-itemA-icon453,
string12131-itemB-icon4535,string22-itemC-icon443

How do I get the following only using only regex? 仅使用正则表达式如何获取以下内容?

string12131-itemB-icon4535,

All numbers are unknown. 所有数字均未知。 The only known parts are itemA, itemB, itemC, string and icon 唯一已知的部分是itemA,itemB,itemC,字符串和图标

I've tried string.+?itemB.+?, but it also picks up from the first occurrence of string rather than the one adjacent to itemB 我尝试过string.+?itemB.+?,但是它也从string的第一次出现而不是与itemB相邻的string提取

I've also tried using [^icon] preceding the itemB in various positions but couldn't get it to work. 我也尝试过在各个位置在itemB之前使用[^icon] ,但无法使其正常工作。

尝试这个:

string[^,]+itemB[^,]+,

试试这个正则表达式

 string\d+-itemB-icon\d+,

The given solutions that use a restricted set of characters instead of a wildcard are simplest, but to get more at the general question: You got the non-greedy quantifier part right, but being non-greedy doesn't prevent the matcher from taking as many characters as it needs to find a match. 给定的解决方案使用有限的字符集而不是通配符是最简单的,但要获得更多有关以下一般性问题的信息:非贪婪量词部分正确,但非贪婪并不能阻止匹配者将其视为找到匹配项需要许多字符。 You might be looking for the atomic group operator, (?>group). 您可能正在寻找原子组运算符(?>group). Once the group matches something, it will be treated atomically if the matcher needs to backtrack. 一旦组匹配了某项,如果匹配器需要回溯,它将被原子地处理。

(?>string.+?item)B.+?,

In your example, the group matches string331-item , but the B doesn't match R so the whole group is tossed and the search moves to the next string . 在您的示例中,组与string331-item匹配,但是BR不匹配,因此整个组被扔掉,搜索移至下一个string

You don't mention the commas separating items as a known part but use it in the example regex so I assume it can be used in a solution. 您没有将逗号分隔为已知部分,而是在示例正则表达式中使用了逗号,因此我认为它可以在解决方案中使用。 Try excluding the comma as a character set instead of matching against ".". 尝试排除逗号作为字符集,而不是与“。”匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM