What regex can match sequences of the same character?
Sure thing! Grouping and references are your friends:
(.)\1+
Will match 2 or more occurences of the same character. For word constituent characters only, use \w
instead of .
, i.e.:
(\w)\1+
Note that in Perl 5.10 we have alternative notations for backreferences as well.
foreach (qw(aaa bbb abc)) {
say;
say ' original' if /(\w)\1+/;
say ' new way' if /(\w)\g{1}+/;
say ' relative' if /(\w)\g{-1}+/;
say ' named' if /(?'char'\w)\g{char}+/;
say ' named' if /(?<char>\w)\k<char>+/;
}