Use brackets to memorize an chunk, and then \1, \2, etc to refer to the memorized chunk inside the regex ($1, $2 outside it)
For example, to search for words which have the same letter twice;
This generates:my $string = <>; $string =~ /(\w*(\w)\w*\2\w*)/; # The beginning of the word, then a notable letter, # maybe some middle letters, then that same notable letter as before, # then the rest of the word. print "Found word '$1', with duplicated letter '$2'\n";
>image imagination dream Found word 'imagination', with duplicated letter 'n' >
If you want to find the duplicated letter which appears first rather than last, change the first \w* to the non-greedy form \w*?. It should be easy to extend this principle to whole sentences/strings.
In reply to Re: regexp to match repeated charater
by SuicideJunkie
in thread regexp to match repeated charater
by ramprasad_gk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |