我在正则表达式中使用非捕获组,即(?:.*)
,但它不起作用。
我仍然能够在结果中看到它。如何忽略它/不在结果中捕获?
代码:
import re
text = '12:37:25.790 08/05/20 Something P LR 0.156462 sccm Pt 25.341343 psig something-else'
pattern = ['(?P<time>\d\d:\d\d:\d\d.\d\d\d)\s{1}',
'(?P<date>\d\d/\d\d/\d\d)\s',
'(?P<pr>(?:.*)Pt\s{3}\d*[.]?\d*\s[a-z]+)'
]
result = re.search(r''.join(pattern), text)
输出:
>>> result.group('pr')
'Something P LR 0.156462 sccm Pt 25.341343 psig'
预期输出:
'Pt 25.341343 psig'
更多信息:
>>> result.groups()
('12:37:25.790', '08/05/20', 'Something P LR 0.156462 sccm Pt 25.341343 psig')
(?:.*)
https://regex101.com/r/X69k0V/1 并且如果数字不能为可选项则为(?P<pr>Pt\s{3}\d+(?:\.\d+)?\s[a-z]+)
。 - The fourth bird.*\b(?P<pr>Pt\s{3}\d+(?:\.\d+)?\s[a-z]+)
https://regex101.com/r/sA3atN/1 - The fourth bird(?P<time>\d\d:\d\d:\d\d.\d\d\d)\s{1}(?P<date>\d\d/\d\d/\d\d)\s.*?(?P<pr>Pt\s{3}\d*[.]?\d*\s[a-z]+)
https://regex101.com/r/dQULqz/1 - The fourth bird