1

How can I match only two words that are separated by one or more spaces?

[\w]+[\s]+[\w]+

matches:

one two
one two three //but this should not match as it countains more than 2 words
Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125
membersound
  • 81,582
  • 193
  • 585
  • 1,120

1 Answers1

5

You can use this regex with line start and end anchors :

^\\s*\\w+\\s+\\w+\\s*$

Note use of ^ and $ that makes sure only two word string, separated by space is matched.

RegEx Reference: http://regular-expressions.info

Community
  • 1
  • 1
anubhava
  • 761,203
  • 64
  • 569
  • 643