谁能告诉我为什么 Python 中的正则表达式模式大部分时间都有效,但对以下文本有困难。
图案:
patternd = re.compile(r"""\(VFSCAN\)[^=]*=\s* # first line of a section: (VFSCAN) AT TIME = 1.1800 UP TO 100 BUSES WITH LOW VOLTAGE DEVIATION BELOW -0.200
(\d*(?:\.\d+)?) # group 1 - first number of first line: 1.1800
\D+
\d+ # second number of first line: 100
\s+BUSES\s+WITH\s+LOW\s+VOLTAGE\s+(DEVIATION)\s+BELOW.*?
\D+ # skip second line
(?: # a data line: 18436 [LENZIE 618.0] -0.245 18433 [LENZIE 318.0] -0.245
(?:\d+\s+\[(.+?)\]\s+\S+\s*)+ # Component of data line
(?=[\r\n\s]+|$)
)+ # This search ends with an empty line
""", re.VERBOSE)
我遇到问题的文字是:
test3 = r'''(VFSCAN) AT TIME = 1.1800 UP TO 100 BUSES WITH LOW VOLTAGE DEVIATION BELOW -0.200:
X ----- BUS ------ X VDEV X ----- BUS ------ X VDEV
18436 [LENZIE 618.0] -0.245 18433 [LENZIE 318.0] -0.245
18431 [LENZIE 118.0] -0.214 18435 [LENZIE 518.0] -0.214
18434 [LENZIE 418.0] -0.214 18432 [LENZIE 218.0] -0.214
(VFSCAN) AT TIME = 1.5167 UP TO 100 BUSES WITH LOW VOLTAGE DEVIATION BELOW -0.200:
X ----- BUS ------ X VDEV X ----- BUS ------ X VDEV
69036 [DNLP2G21.575] -0.414 69038 [DNLP2G22.575] -0.414
69040 [DNLP2G23.575] -0.414 69032 [DNLP1_G1.575] -0.402
65460 [DIFICULT 230] -0.384 69027 [7MIHL G1.575] -0.355
69076 [HORIZ_G .575] -0.303 67237 [MEDBOWCO 115] -0.301
67940 [STNDPSVC 230] -0.300 65976 [MINERS 34.5] -0.294
65585 [FT CRK1 34.5] -0.261 65584 [FT CRK2 34.5] -0.261
69073 [HIPLN_G .575] -0.214
(VFSCAN) AT TIME = 1.1800 UP TO 100 BUSES WITH LOW VOLTAGE DEVIATION BELOW -0.200:
X ----- BUS ------ X VDEV X ----- BUS ------ X VDEV
65191 [BONANZA 24.0] -0.572 65192 [BONANZA 138] -0.434
65194 [CHAPITA 138] -0.433 66278 [RANGELY 138] -0.320
65371 [COVE TP 138] -0.302 79265 [CALAMRDG 138] -0.286
79400 [DES.MINE 138] -0.285 65086 [ASHLEY 69.0] -0.284
79067 [VERNAL 138] -0.277 67257 [MOONLAK269.0] -0.268
67256 [MOONLAK169.0] -0.266 79264 [W.RV.CTY 138] -0.206
'''
我正在使用“findall”使用上面的模式匹配文本,并期望提取一个元组列表。当我运行它时,我得到:
[('1.1800', 'DEVIATION', 'LENZIE 218.0'), ('1.5167', 'DEVIATION', 'HIPLN_G .575'), ('1.1800', 'DEVIATION', 'W.RV.CTY 138')]
但我应该在我的列表中获得更多匹配的元组。我真的只对提取每场比赛第一行的时间以及方括号“[]”中的所有项目感兴趣。我应该在列表中得到 6+13+12 = 31 个元组。任何帮助,将不胜感激。