I want to check if number of words between two strings in a larger string is < n. For example, suppose I have a string like "a quick brown fox jumps over the lazy dog". I want to see if the distance between strings "brown fox" and "lazy dog" is < 5 in the larger string. What would be a proper python regexp for that?
I tried as per the link. My code was
s='a quick brown fox jumps over the lazy dog'
m=re.search("'brown fox'\W+(?:\w+\W+){1,4}'lazy dog'",s)
but there was no match.
'
are not part of the character class\w
, and so something like\S
(not-space) might be more appropriate. – Patashu