ruby - How do I match repeated characters? -
how find repeated characters using regular expression?
if have aaabbab, match characters have 3 repetitions:
aaa
try string.scan(/((.)\2{2,})/).map(&:first), string string of characters.
the way works looks character , captures (the dot), matches repeats of character (the \2 backreference) 2 or more times (the {2,} range means "anywhere between 2 , infinity times"). scan return array of arrays, map first matches out of desired results.
Comments
Post a Comment