正则表达式搜索Python的响应
我正在尝试使用一个python脚本,它应该匹配一个正则表达式,由much生成的对象总是一个空字符串,因此我认为我使用的正则表达式没有必要的知识。有人能帮忙吗?代码如下:正则表达式搜索Python的响应,python,regex,python-2.7,Python,Regex,Python 2.7,我正在尝试使用一个python脚本,它应该匹配一个正则表达式,由much生成的对象总是一个空字符串,因此我认为我使用的正则表达式没有必要的知识。有人能帮忙吗?代码如下: def storecheck(text, store): res ="" for line in text.splitlines(): print str(store) if re.search('/'+str(store)+',/g', line): print re.search('/'+st
def storecheck(text, store):
res =""
for line in text.splitlines():
print str(store)
if re.search('/'+str(store)+',/g', line):
print re.search('/'+str(store)+',/g', line)+';'
res+= line
return res
“store”在脚本中的值为整数。
我已经在python官方网站上阅读了re.match和re.search的手册,但是只要这些都不是幻觉,这个regexp就应该匹配。有人知道我做错了什么吗?Python不需要分隔符。。。如果需要全局搜索,可以使用
re.findall
。也许你打算做那样的事
def storecheck(text, store):
res = ""
for line in text.splitlines():
print str(store)
if re.search(str(store), line):
print ';'.join(re.findall(str(store), line))
res += line
return res
不过,我自己对python还是相当陌生;有更好的方法可以做同样的事情。为什么不把store=str(store)
放在函数的顶部,这样你就不用一直调用它了?