尝试从 python 中的正则表达式匹配打印组
trying to print a group from a regex match in python
我正在尝试从我的正则表达式匹配中打印组信息。
我的脚本匹配我的正则表达式与文件中的行,所以这是有效的。
我是根据 python 正则表达式教程顺便说一句...
我是 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()
returns 匹配对象 - 您需要将其分配给某物。尝试
for line in file:
m = p.match(line)
if m:
print m.group()
i += 1
您没有使用匹配返回的正则表达式对象。试试这个:
for line in file:
matched = p.match(line)
if matched:
print matched.group() # this should now work
i += 1
我正在尝试从我的正则表达式匹配中打印组信息。 我的脚本匹配我的正则表达式与文件中的行,所以这是有效的。
我是根据 python 正则表达式教程顺便说一句... 我是 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()
returns 匹配对象 - 您需要将其分配给某物。尝试
for line in file:
m = p.match(line)
if m:
print m.group()
i += 1
您没有使用匹配返回的正则表达式对象。试试这个:
for line in file:
matched = p.match(line)
if matched:
print matched.group() # this should now work
i += 1