Im trying to make a regular expression in python that allows me to find a word within a string “n” times
For example, if i wanted to find a expression that could match if the word “cat” is exactly two times. How i would do that?
It should accept “The blue cat talks to the red cat in the tree”. Because it has “cat” exactly two times.
But it should not accept “The cat is big”. Because it has “cat” only once
And it should not accept either “the dog is yellow”. For similar reasons
Thanks a lot
EDIT
Hey guys
Sorry for complicating the problem too much, but i forgot to mention one thing.
If i wanted to find “cat” exactly two times, “The catcat runs” would also match
Don’t use regular expressions just because they are there.
As Vincent points out, that assumes all words are seperated by whitespace.
Is probably a better options. Although whether that is neccesary depends on details not provided in your post.
EDIT
If you don’t even care about word boundaries, there is even less reason to be using a regular expression.