如何匹配字符串中任何字符的奇数?

时间:2019-03-17 09:41:39

标签: c# regex

我正在寻找一种纯正则表达式解决方案,以获取由相似字符组成的奇数长度子字符串。

例如,我的字符串:

hjasaaasjasjbbbbbashjasccccccc

因此,匹配结果应为:

[aaa],[bbbbb],[ccccccc]

到目前为止,我已经尝试过:

(?<!\1\1)*(?<!\1)(.)(\1\1)*(?:\1\1)*(?!\1)

但是它不起作用。

1 个答案:

答案 0 :(得分:3)

对于与任何字符的奇数匹配(仅 个字符匹配)的仅用于正则表达式的解决方案:

(.)(?<!\1\1)\1(?:\1\1)*\1(?!\1)

或更短的版本thanks to Wiktor

(.)(?<!\1\1)(?:\1\1)+(?!\1)

Demo

故障:

(.)         # First capturing group - matches any character.
(?<!\1\1)   # Negative lookbehind - ensures the matched char isn't preceded by the same char.
(?:\1\1)    # A non-capturing group that matches two occurrences of 
            # the same char (at least 3 in total).
+           # Matches between one and unlimited times of the previous group.
(?!\1)      # Negative lookahead to make sure no extra occurrence of the char follows.

C#中的演示:

string input = "hjasaaasjasjbbbbbashjasccccccc";
string pattern = @"(.)(?<!\1\1)(?:\1\1)+(?!\1)";
var matches = Regex.Matches(input, pattern);
foreach (Match m in matches)
    Console.WriteLine(m.Value);

输出:

aaa
bbbbb
ccccccc

如果您想要一个与任何字符的奇数匹配的解决方案(包括个字符的匹配):

(.)(?<!\1\1)(?:\1\1)*(?!\1)

Demo