regex: matching 3 consecutive words

Question:

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?

Asked By: Subbeh

||

Answers:

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]+
Answered By: heemayl

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.

Answered By: Antoine

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

([^s]+ ){2}[^s]+
Answered By: Benedict Harris
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.