Reputation: 2514
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.
Upvotes: 1
Views: 1649
Reputation: 62908
You are trying to match for single quotes, but there aren't any in the string:
>>> re.search("brown fox\W+(?:\w+\W+){1,4}lazy dog", s)
<_sre.SRE_Match at 0x3045850>
>>> re.search("brown fox\W+(?:\w+\W+){1,3}lazy dog", s)
<_sre.SRE_Match at 0x3045920>
>>> re.search("brown fox\W+(?:\w+\W+){1,2}lazy dog", s)
(None)
Upvotes: 4