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
  • 85,718
  • 5
  • 90
  • 121
membersound
  • 74,158
  • 163
  • 522
  • 986

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
  • 713,503
  • 59
  • 514
  • 593