简体   繁体   English

为什么这个正则表达式不匹配完整的模式?

[英]Why doesn't this regex match the full patterns?

import re

patterns = re.compile(r'(yesterday|today) \d{1,2} hours \d{1,2} minutes')

matches = re.findall(patterns, 'yesterday 9 hours 32 minutes today 10 hours 30 minutes')

print(matches)

The print output of the code above is:上面代码的打印 output 是:

['yesterday', 'today']

I hope it is:我希望它是:

['yesterday 9 hours 32 minutes', 'today 10 hours 30 minutes']

Why doesn't it match the full patterns?为什么它不匹配完整的模式?

You are using your initial capture group to designate the choice between yesterday and today:您正在使用初始捕获组来指定昨天和今天之间的选择:

(yesterday|today) -- grouping is a valid use of the capture group, but in this case it's having the unintended consequence of confusing you. (yesterday|today) ——分组是对捕获组的有效使用,但在这种情况下,它会产生让您感到困惑的意外后果。

You can handle this several ways.您可以通过多种方式处理此问题。 The following will get you the result you want using finditer and a reference to .group(0) which always indicates the full matched text:以下内容将使用 finditer 和对始终指示完整匹配文本的 .group .group(0)的引用为您提供所需的结果:

import re

patterns = re.compile(r'(yesterday|today) \d{1,2} hours \d{1,2} minutes')

matches = patterns.finditer('yesterday 9 hours 32 minutes today 10 hours 30 minutes')
for match in matches:
    print(match.group(0))

You could also do something like:你也可以这样做:

import re

patterns = re.compile(r'(?:yesterday|today) \d{1,2} hours \d{1,2} minutes')

matches = patterns.findall('yesterday 9 hours 32 minutes today 10 hours 30 minutes')
print(matches)

Which will convert the capture group to a non-capture group.这会将捕获组转换为非捕获组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM