-2

What is the exact difference between these regular expressions?

First:

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

Second:

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

Third:

\\b(\\w+) \\1
Azar
  • 1,086
  • 12
  • 27
HussainBiedouh
  • 99
  • 2
  • 11
  • 3
    I recommend you try them out yourself and see. Check out the bottom of the [Stack Overflow Regular Expression FAQ](http://stackoverflow.com/a/22944075/2736496), which contains a listing of many online testers you can use. Try out a bunch of different inputs and I think the difference will become obvious. In particular, check out the "Anchors" section, which contains this answer: [`\b`:word boundary, and `\B`:non-word boundary](http://stackoverflow.com/a/6664167). – aliteralmind Apr 10 '14 at 15:28
  • What @aliteralmind said; If you are seeing the same behavior, you don't appear to be testing very thoroughly. – Andrew Barber Apr 10 '14 at 15:29

1 Answers1

1
  1. \\b(\\w+) \\1\\b matches a word, a space, and the same word again, flanked by word boundaries. For instance, it would match "a a" in "a a ", but would not match anything in "aa-", "aab", or "a ab".

  2. \\b(\\w+)\\1\\b matches a word, and the same word again, flanked by word boundaries. For instance, it would match "aa", in "aa-" but would not match anything in "aab" or "a a".

  3. \\b(\\w+) \\1 matches a word, a space, and the same word again, but only needs a word boundary at the start. For instance, it would match "a a" in "a ab", but nothing in "aa" or "aab"

AJMansfield
  • 4,039
  • 3
  • 29
  • 50