the second line does something that may not be obvious to everyone, and seems to duplicate /g 's functionality. However, since you've (seemingly) got 3 gamma in a row, writing$_="alpha beta beta gamma gamma gamma"; while (s/((\w+)\s\2)/$2/) {}; print $_;
Will leave you with an extra gamma. Using the 'useless' while loop allows the regex to check for multiple duplicates.$_="alpha beta beta gamma gamma gamma"; s/((\w+)\s\2)/$2/g; print $_;
As for the regex you tried $string =~ s/(\w+)(.*)\b\1/$1 $2/sig;, we have :
In reply to Re: most efficient regex to delete duplicate words (boo)
by boo_radley
in thread most efficient regex to delete duplicate words
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |