6

I'm trying to see if a string contains 3 consecutive words (divided by spaces and without numbers), but the regex I have constructed does not seem to work:

print re.match('([a-zA-Z]+\b){3}', "123 test bla foo")
None

This should return true since the string contains the 3 words "test bla foo".

What is the best way to achieve this?

Subbeh
  • 886
  • 2
  • 7
  • 14
  • As a side note: use `re.search()` instead of `re.match()` - the latter would start from the beginning of a string. – alecxe Jan 17 '17 at 04:06

3 Answers3

11

Do:

(?:[A-Za-z]+ ){2}[A-Za-z]+
  • (?:[A-Za-z]+ ){2}: the non-captured group (?:[A-Za-z]+ ) matches one or more alphabetic characters followed by space, {2} matches two such successive groups

  • [A-Za-z]+ matches one or more alphabetic character after the preceding two words, making the third word

Demo

If you want the words to be separated by any whitespace instead of just space:

(?:[A-Za-z]+\s){2}[A-Za-z]+
heemayl
  • 39,294
  • 7
  • 70
  • 76
0

I use this to select the first words of a string:

^(?:[^\ ]+\ ){3}

I use the whitespaces for define and delimite each words.

[^\ ]+: minimum one char except whitespaces, followed by an whitespace \. After you juste have to enter the number of words you want : {3}

It works very well.

Antoine
  • 13
  • 4
0

this is a much better option.
It includes words with hyphens or apostrophe, like "don't" or "mother-in-law"

([^\s]+ ){2}[^\s]+
Benedict Harris
  • 184
  • 1
  • 9