Regular Expression For Duplicate Words

Joshua picture Joshua · May 12, 2010 · Viewed 95.3k times · Source

I'm a regular expression newbie, and I can't quite figure out how to write a single regular expression that would "match" any duplicate consecutive words such as:

Paris in the the spring.

Not that that is related.

Why are you laughing? Are my my regular expressions THAT bad??

Is there a single regular expression that will match ALL of the bold strings above?

Answer

Gumbo picture Gumbo · May 12, 2010

Try this regular expression:

\b(\w+)\s+\1\b

Here \b is a word boundary and \1 references the captured match of the first group.