我必须在文件中搜索s="xyz-123."
。 s
来自其他一些代码
import re
s="xyz-123." # This will come from some other code
f=open("\path","r")
searched_item = re.findall(r's[-]+?[0-9a-zA-Z-]+[-]+[a-zA-Z0-9]+[.]+[a-zA-Z0-9]+[.]+[a-zA-Z0-9]+', f.read())
答案 0 :(得分:1)
如果s
是固定字符串,那么在字符串中使用find
而不是涉及regex
def findall(p, s):
'''Yields all the positions of
the pattern p in the string s.'''
i = s.find(p)
while i != -1:
yield i
i = s.find(p, i+1)
import re
s="xyz-123." # This will come from some other code
f=open("\path","r")
file_content = f.read()
for match_index in findall(s, file_content):
print match_index