1

I am looking for a purely Regex solution to get odd length substring made up of similar characters.

For example, my string:

hjasaaasjasjbbbbbashjasccccccc

So, the resulting matches should be:

[aaa],[bbbbb],[ccccccc]

So far, I have tried:

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

But it's not working.

user786
  • 3,502
  • 3
  • 28
  • 56

1 Answers1

3

For a regex-only solution that matches an odd number of any character (excluding one-character matches):

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

Or a shorter version thanks to Wiktor:

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

Demo.

Breakdown:

(.)         # 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.

Demo in 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);

Output:

aaa
bbbbb
ccccccc

If you want a solution that matches an odd number of any character (including one-character matches):

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

Demo.