正则表达式搜索Python的响应

2024-03-29 00:23:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试编写一个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('/'+str(store)+',/g', line)+';'
        res+= line
        return res

“store”在脚本中的值为整数。 我读过重新匹配以及搜索在python的官方站点中,但是只要online tester不是幻觉,这个regexp应该匹配。有人看到我做错什么了吗?在


Tags: 对象store字符串代码textre脚本search
1条回答
网友
1楼 · 发布于 2024-03-29 00:23:34

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还是相当陌生的;有更好的方法来做同样的事情。在

相关问题 更多 >