1
votes

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.

2
Don't forget that characters like ' are not part of the character class \w, and so something like \S (not-space) might be more appropriate.Patashu

2 Answers

4
votes

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)
2
votes

Try brown fox\s+(\S+\s+){0,4}lazy dog