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

Popular posts from this blog

multithreading - Exception in Application constructor -

React Native allow user to reorder elements in a scrollview list -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -