Python Regular Expression to match multiple occurrences of word

时间:2015-05-24 21:20:50

标签: python regex

I want to use re.search (or, more precisely, pandas.str.contains) to match a string whenever it contains the word 'car' at least two times (or exactly two times, whatever is easier). As an example, consider the two strings

string1 = 'Car, Cat, House, Car'
string2 = 'Car, Cat, House'

The regex expression I am searching for should match string1, but not string2. I was playing around with look ahead (things like Car(?=Car)), but got completely weird behaviour (unsurprisingly, I never got a handle on regex...).

1 个答案:

答案 0 :(得分:2)

The pattern you are looking for is Car.*?Car, that is two occurences of Car, separed by anything (or nothing at all). This will match any string containing at least two occurences of Car:

re.search(r"Car.*?Car", string1)

The non-greedy star *? will make sure re.search stops as soon as two occurences are found.

If string1 contains line terminators, also add the re.MULTILINE flag.