Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find repeated letters with a Perl regex?

Tags:

I am looking for a regex that will find repeating letters. So any letter twice or more, for example:

booooooot or abbott 

I won't know the letter I am looking for ahead of time.

This is a question I was asked in interviews and then asked in interviews. Not so many people get it correct.

like image 507
Brian G Avatar asked Oct 07 '08 14:10

Brian G


People also ask

What does =~ do in Perl?

Look it up on a text on Perl. Use parentheses. The ( =~ ) operator takes two arguments: a string on the left and a regular expression pattern on the right. Instead of searching in the string contained in the default variable, $_ , the search is performed in the string specified.

What is \W in Perl regex?

A \w matches a single alphanumeric character (an alphabetic character, or a decimal digit) or _ , not a whole word. Use \w+ to match a string of Perl-identifier characters (which isn't the same as matching an English word).

How do I search for a pattern in Perl?

m operator in Perl is used to match a pattern within the given text. The string passed to m operator can be enclosed within any character which will be used as a delimiter to regular expressions.


2 Answers

You can find any letter, then use \1 to find that same letter a second time (or more). If you only need to know the letter, then $1 will contain it. Otherwise you can concatenate the second match onto the first.

my $str = "Foooooobar";  $str =~ /(\w)(\1+)/;  print $1; # prints 'o' print $1 . $2; # prints 'oooooo' 
like image 161
Adam Bellaire Avatar answered Jan 01 '23 18:01

Adam Bellaire


I think you actually want this rather than the "\w" as that includes numbers and the underscore.

([a-zA-Z])\1+ 

Ok, ok, I can take a hint Leon. Use this for the unicode-world or for posix stuff.

([[:alpha:]])\1+ 
like image 41
Keng Avatar answered Jan 01 '23 17:01

Keng