Category: >> Perl Code >> Regexp: Find and highlight repeated words Bookmark and Share

<< lastnext >>

Snippet Name: Regexp: Find and highlight repeated words

Description: This pattern is designed to match a word followed by white-space and then the same word again. This matched pattern is then replaced by itself wrapped in square-brackets.

Also see:
» Count all words in a string
» Count specific characters in a str...

Comment: (none)

Author: CoderZone
Language: PERL
Highlight Mode: PERL
Last Modified: December 03rd, 2010

$str = "we will will find any repeated words here here...";
$str =~ s/(\b(\w+)\s+\2\b)/[$1]/g;
print $str;
There haven't been any comments added for this snippet yet. You may add one if you like.  Add a comment 
© | users online: 5