我有一个字符串:
aaabbashasccddee
我想获得偶数个连续相同字符的匹配项。例如,从上面的字符串中,我想要这些匹配项:
[bb],[cc],[dd],[ee]
我已经尝试过此解决方案,但还没有结束:
"^(..)*$
请帮助
答案 0 :(得分:5)
幸运的是,.NET正则表达式能够处理无限的回顾。您可以使用以下正则表达式来实现所需的功能:
((?>(?(2)(?=\2))(.)\2)+)(?<!\2\1)(?!\2)
正则表达式细目:
(
开始捕获#1组
(?>
非捕获组的开始(原子)
(?(2)
如果设置了捕获组#2
(?=\2)
应该是下一个字符)
有条件的结束(.)\2
匹配并捕获一个字符,然后再次匹配它(偶数))+
尽可能重复,至少重复一次)
捕获#1组结束(?<!\2\1)
这是窍门。后退告诉引擎,比我们到目前为止匹配的字符早的前一个字符不应与存储在捕获组#2中的相同字符(?!\2)
下一个字符不应与捕获组#2中存储的字符相同更新:
因此,您可以在C#中执行以下代码,以Regex
的形式获取字符串中所有偶数序列char,而无需任何其他运算符(纯正则表达式)。
var allEvenSequences = Regex.Matches("aaabbashasccddee", @"((?>(?(2)(?=\2))(.)\2)+)(?<!\2\1)(?!\2)").Cast<Match>().ToList();
此外,如果您想创建[bb],[cc],[dd],[ee]
,则可以加入该序列数组:
string strEvenSequences = string.Join(",", allEvenSequence.Select(x => $"[{x}]").ToArray());
//strEvenSequences will be [bb],[cc],[dd],[ee]
答案 1 :(得分:2)
另一种不涉及条件的可能的仅用于正则表达式的解决方案:
(.)(?<!\1\1)\1(?:\1\1)*(?!\1)
故障:
(.) # First capturing group - matches any character.
(?<!\1\1) # Negative lookbehind - ensures the matched char isn't preceded by the same char.
\1 # Match another one of the character in the 1st group (at least two in total).
(?:\1\1) # A non-capturing group that matches two occurrences of the same char.
* # Matches between zero and unlimited times of the previous group.
(?!\1) # Negative lookahead to make sure no extra occurrence of the char follows.
演示:
string input = "aaabbashasccddee";
string pattern = @"(.)(?<!\1\1)\1(?:\1\1)*(?!\1)";
var matches = Regex.Matches(input, pattern);
foreach (Match m in matches)
Console.WriteLine(m.Value);
输出:
bb
cc
dd
ee