Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex: matching 3 consecutive words

Tags:

python

regex

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?

like image 532
Subbeh Avatar asked Jan 17 '17 03:01

Subbeh


People also ask

How do you repeat in regex?

A repeat is an expression that is repeated an arbitrary number of times. An expression followed by '*' can be repeated any number of times, including zero. An expression followed by '+' can be repeated any number of times, but at least once.

Is regex matching fast?

Regular expression matching can be simple and fast, using finite automata-based techniques that have been known for decades. In contrast, Perl, PCRE, Python, Ruby, Java, and many other languages have regular expression implementations based on recursive backtracking that are simple but can be excruciatingly slow.

Can you use F string in regex?

Can you use an F-string in regex Python? in which we look at one or two ways to make life easier when working with Python regular expressions. tl;dr: You can compose verbose regular expressions using f‍-‍strings.

What is the regular expression matching one or more specific characters?

The character + in a regular expression means "match the preceding character one or more times". For example A+ matches one or more of character A. The plus character, used in a regular expression, is called a Kleene plus .


1 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]+
like image 135
heemayl Avatar answered Sep 28 '22 15:09

heemayl