假设可能的表达式值如下:
a) 10-15 of 25 results
b) 20-25 of 25 results
c) 1,220 - 1,240 of 1300 results
d) 1,280 - 1,300 of 1300 results
我想测试表达式,使得b和d将返回true,而a和c将返回false。换句话说,我正在寻找序列条件的结束。
答案 0 :(得分:1)
这应该可以解决问题:
end_re = re.compile(r'([\d,]+) of \1 results')
def is_end(s):
return bool(end_re.search(s))
想法是在结果的最后部分找到相同的数字。 See this answer for more info
编辑:用提问者提到的逗号修复问题
答案 1 :(得分:1)
感谢Pykler指出我正确的方向。答案必须考虑到逗号,所以我对逗号进行了以下修改:
def is_end( s ) :
end_re = re.compile(r'([\d]*,?[\d]*) of \1 results')
return bool(end_re.search(s))
答案 2 :(得分:0)
这有效 -
input = '''
a) 10-15 of 25 results
b) 20-25 of 25 results
c) 1,220 - 1,240 of 1300 results
d) 1,280 - 1,300 of 1300 results
'''
import re
regex = r'([0-9\,]+)[ ]*\-[ ]*([0-9\,]+)[ ]+of[ ]+([0-9]+)'
for ip in input.split("\n"):
matches = re.findall(regex, ip)
if matches and len(matches[0]) == 3:
r = int(matches[0][1].replace(",",""))
results = int(matches[0][2])
if(r == results):
print "true"
else:
print "false"
'''OUTPUT:
false
true
false
true
'''