$foo =~ m/\A(\W) # \A instead of ^ and match first non-word \w+\1 # Match any word char until the next \1 (\W) # Match following non-word /xms;