Для решения только для регулярных выражений, которое соответствует нечетному числу любого символа ( исключая однозначные совпадения):
(.)(?<!\1\1)\1(?:\1\1)*\1(?!\1)
Или более короткая версия благодаря Wiktor:
(.)(?<!\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\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)
Демо .