[英]counting number of words between two strings python regexp
I want to check if number of words between two strings in a larger string is < n. 我想检查较大字符串中两个字符串之间的单词数是否<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.
我想看看较大的字符串中字符串“棕狐”和“懒狗”之间的距离是否小于5。 What would be a proper python regexp for that?
什么是适当的python regexp?
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. 但没有比赛。
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)
尝试brown fox\\s+(\\S+\\s+){0,4}lazy dog
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.