s/^ # Start of string ( # Start remembering \W* # Leading non word characters (\w+) # First word of string \s+ # Whitespace (\w+) # Second word of string \b # Make sure we got the entire word .*? # Skip till second occurance ) # Stop remembering \b # Start at the beginning of a word \2 # Repeat of the first word \s+ # Whitespace \3 # Repeat of the second word \b # And that's the end of the word /$1/sx; # Just keep what we remembered