正则表达式搜索Python的响应

时间:2013-10-31 16:18:26

标签: python regex python-2.7

我正在尝试使用一个与正则表达式匹配的python脚本,由the many生成的对象总是一个空字符串,所以我想我正在使用没有所需知识的正则表达式。有人可以帮忙吗?以下是代码:

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的手册,但只要online tester不是一个幻觉,这个正则表达式就应该匹配。所以任何人都能看到我做错了什么?

1 个答案:

答案 0 :(得分:1)

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很新;可能有更好的方法来做同样的事情。

相关问题