Subbeh
Subbeh

Reputation: 924

regex: matching 3 consecutive words

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?

Upvotes: 6

Views: 18287

Answers (3)

Benedict Harris
Benedict Harris

Reputation: 304

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

([^\s]+ ){2}[^\s]+

Upvotes: 0

Antoine
Antoine

Reputation: 13

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.

Upvotes: 0

heemayl
heemayl

Reputation: 42037

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]+

Upvotes: 11

Related Questions