是否有一种优雅的单行方法可以找到一个包含7的数字,除非它只是7?
if re.search(r'(\d+)?(7)(\d+)', line):
print "Found 7 inside or beginning of a number", match.group()
else:
if re.search(r'(\d+)(7)(\d+)?', line):
print "Found 7 in the end of a number", match.group()发布于 2013-09-05 00:02:44
你是说
'7' in line and len(line) > 1您还可以使用str.isdigit()检查是否所有字符都是数字。
发布于 2013-09-05 00:45:30
这里有一个涉及regex的简单解决方案,因为您特别要求:
re.search("(7.)|(.7)", line)https://stackoverflow.com/questions/18625699
复制相似问题