The reason that βaβ is peeping is because it consumes the first βaβ (but does not capture it), then it captures the rest.
Will this template work for you? New pattern: \ba+(.+)\b It uses the word boundary \b to anchor both ends of the word. It corresponds to at least one βaβ, followed by the remaining characters until the end of the word boundary. The remaining characters are written to the group, so you can easily refer to them.
string pattern = @"\ba+(.+)\b"; foreach (Match m in Regex.Matches("aaabbbb", pattern)) { Console.WriteLine("Match: " + m.Value); Console.WriteLine("Group capture: " + m.Groups[1].Value); }
UPDATE:. If you want to skip the first occurrence of any duplicated letters, then match the rest of the line, you can do this:
string pattern = @"\b(.)(\1)*(?<Content>.+)\b"; foreach (Match m in Regex.Matches("aaabbbb", pattern)) { Console.WriteLine("Match: " + m.Value); Console.WriteLine("Group capture: " + m.Groups["Content"].Value); }
Ahmad mageed
source share