我试图在大文本文件中找到8位数字(有时中间有空格)。你知道吗
我首先测试8位数字是否存在,然后尝试列出它们。你知道吗
import re
test_str = '''
123456789123
1242232
'''
def searchfor8digits(input_string):
regex = r'\d{8}|\d{4}\s\d{4}'
matches = re.finditer(regex, input_string)
for matchNum, match in enumerate(matches):
matchNum = matchNum + 1
print("Match {matchNum} was found at {start}-{end}: {match}".format(
matchNum=matchNum, match=match.group(),
start=match.start(), end=match.end()))
def contains8digits(input_string):
regex = r'\d{8}|\d{4}\s\d{4}'
matches = re.finditer(regex, input_string)
if matches:
return True
else:
return False
if __name__ == '__main__':
if contains8digits(test_str):
searchfor8digits(test_str)
有没有更好的办法?你知道吗
有没有更好的/更像Python的方法来检查8位数字是否存在?你知道吗
另外,如果输入字符串太大/太长,如何更好地进行测试?你知道吗
目前没有回答
相关问题 更多 >
编程相关推荐