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?
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.
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 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.
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 .
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]+
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With