在 Python 正则表达式问题中捕获组
Capture groups in Python regex issue
我正在文本文件中搜索字符串 Number of copies:
,一旦找到,我想打印与之关联的数字,所以我正在搜索 (\sNumber of copies: (\d{1,2}))
并且我想要return(\d{1,2})
。我一直在研究 REGEX 中的嵌套组,但我没有在 Python 中找到它的语法。任何帮助将不胜感激。
zDiscs = re.search(r'(\sNumber of copies: (\d{1,2}))', fi2Content, re.M|re.I)
print(zDiscs.group(1))
这是我在文本文件中查找的行:
12/13/2013 08:11:25 Number of Discs in Set: 2 - Number of copies: 2
我想要的输出只是 2
和 2
,因为我正在寻找文本后面的数字。
fi2Content
表示 Python.
读取的整个文本文件
我可以 print(zDiscs)
但我不能 print(zDiscs.group(2))
。为什么?
我收到以下错误:
AttributeError: 'NoneType' object has no attribute 'group'
当我尝试 print(zDiscs.group(2))
如果这有助于解决问题,这是我的整个脚本
fo = open('outputFile', 'w')
fo.write("Col1|Col2|Col3\n")
# 1.walk around directory and find lastjob.txt file in one of folders
rootDir = "C:\Users\bob\Desktop\Path Parsing Project"
for path, dirs, files in os.walk(rootDir):
for filename in files:
fullpath = os.path.join(path, filename)
if filename=="text.txt":
print(fullpath)
# 2.open file. read from file
fi2 = open(fullpath, 'r')
fi2Content = fi2.read()
zDiscs = re.search(r'(\sNumber of copies: (\d{1,2}))', fi2Content, re.M|re.I)
print(zDiscs.group(2)) #This is where the error occurs!!!!!!!!!!!!!
您一定在寻找:
import re
zDiscs = re.search(r'(\sNumber of copies: (\d{1,2}))', " 12/13/2013 08:11:25 Number of Discs in Set: 2 - Number of copies: 2", re.I)
print(zDiscs.group(2))
输出:54
请注意,re.M
在您的正则表达式中是多余的,因为您的模式中没有锚点 ^
和 $
(只有它们的行为受该选项影响)。
如果使用(\sNumber of copies: (\d{1,2}))
,则有2个捕获组,编号在第2组。
如果您搜索表达式的文件不包含该文本,并且您想跳过它,请检查您是否获得了匹配对象:
zDiscs = re.search(r'(\sNumber of copies: (\d{1,2}))', fi2Content, re.I)
if zDiscs:
print(zDiscs.group(2))
我正在文本文件中搜索字符串 Number of copies:
,一旦找到,我想打印与之关联的数字,所以我正在搜索 (\sNumber of copies: (\d{1,2}))
并且我想要return(\d{1,2})
。我一直在研究 REGEX 中的嵌套组,但我没有在 Python 中找到它的语法。任何帮助将不胜感激。
zDiscs = re.search(r'(\sNumber of copies: (\d{1,2}))', fi2Content, re.M|re.I)
print(zDiscs.group(1))
这是我在文本文件中查找的行:
12/13/2013 08:11:25 Number of Discs in Set: 2 - Number of copies: 2
我想要的输出只是 2
和 2
,因为我正在寻找文本后面的数字。
fi2Content
表示 Python.
我可以 print(zDiscs)
但我不能 print(zDiscs.group(2))
。为什么?
我收到以下错误:
AttributeError: 'NoneType' object has no attribute 'group'
当我尝试 print(zDiscs.group(2))
如果这有助于解决问题,这是我的整个脚本
fo = open('outputFile', 'w')
fo.write("Col1|Col2|Col3\n")
# 1.walk around directory and find lastjob.txt file in one of folders
rootDir = "C:\Users\bob\Desktop\Path Parsing Project"
for path, dirs, files in os.walk(rootDir):
for filename in files:
fullpath = os.path.join(path, filename)
if filename=="text.txt":
print(fullpath)
# 2.open file. read from file
fi2 = open(fullpath, 'r')
fi2Content = fi2.read()
zDiscs = re.search(r'(\sNumber of copies: (\d{1,2}))', fi2Content, re.M|re.I)
print(zDiscs.group(2)) #This is where the error occurs!!!!!!!!!!!!!
您一定在寻找:
import re
zDiscs = re.search(r'(\sNumber of copies: (\d{1,2}))', " 12/13/2013 08:11:25 Number of Discs in Set: 2 - Number of copies: 2", re.I)
print(zDiscs.group(2))
输出:54
请注意,re.M
在您的正则表达式中是多余的,因为您的模式中没有锚点 ^
和 $
(只有它们的行为受该选项影响)。
如果使用(\sNumber of copies: (\d{1,2}))
,则有2个捕获组,编号在第2组。
如果您搜索表达式的文件不包含该文本,并且您想跳过它,请检查您是否获得了匹配对象:
zDiscs = re.search(r'(\sNumber of copies: (\d{1,2}))', fi2Content, re.I)
if zDiscs:
print(zDiscs.group(2))