gpt4 book ai didi

python - 试图从 python 中的正则表达式匹配中打印一个组

转载 作者:行者123 更新时间:2023-11-28 18:37:26 25 4
gpt4 key购买 nike

我正在尝试从我的正则表达式匹配中打印组信息。我的脚本匹配我的正则表达式与我文件中的行,所以这是有效的。

我是基于 python 正则表达式教程 btw ...我是一个 python 新手(有一些 perl 经验):)

import re

file = open('read.txt', 'r')

p = re.compile("""
.*,\\\"
(.*) # use grouping here with brackets so we can fetch value with group later on
\\\"
""", re.VERBOSE)

i = 0


for line in file:
if p.match(line):
print p.group() #this is the problematic group line
i += 1

最佳答案

re.match() 返回一个匹配对象 - 您需要将其分配给某物。尝试

for line in file:
m = p.match(line)
if m:
print m.group()
i += 1

关于python - 试图从 python 中的正则表达式匹配中打印一个组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31169210/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com