Python正则表达式匹配OR运算符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19821487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 14:43:14  来源:igfitidea点击:

Python regex match OR operator

pythonregexstringtime

提问by Mark Kennedy

I'm trying to match time formats in AM or PM.

我正在尝试匹配 AM 或 PM 中的时间格式。

i.e. 02:40PM
     12:29AM 

I'm using the following regex

我正在使用以下正则表达式

timePattern = re.compile('\d{2}:\d{2}(AM|PM)')

but it keeps returning only AMPMstring without the numbers. What's going wrong?

但它只返回AMPM没有数字的字符串。怎么了?

采纳答案by hwnd

Use a non capturing group (?:and reference to the match group.

使用非捕获组(?:并引用匹配组。

Use re.Ifor case insensitive matching.

使用re.I不区分大小写的匹配。

import re

def find_t(text):
    return re.search(r'\d{2}:\d{2}(?:am|pm)', text, re.I).group()

You can also use re.findall()for recursive matching.

您还可以re.findall()用于递归匹配。

def find_t(text):
    return re.findall(r'\d{2}:\d{2}(?:am|pm)', text, re.I)

See demo

demo

回答by Ashwini Chaudhary

You're not capturing the Hour, minute fields:

您没有捕获小时、分钟字段:

>>> import re
>>> r = re.compile('(\d{2}:\d{2}(?:AM|PM))')
>>> r.search('02:40PM').group()
'02:40PM'
>>> r.search('Time is 12:29AM').group()
'12:29AM'

回答by Ashwini Chaudhary

Use a non-delimited capture group (?:...):

使用非分隔的捕获组(?:...)

>>> from re import findall
>>> mystr = """
... 02:40PM
... 12:29AM
... """
>>> findall("\d{2}:\d{2}(?:AM|PM)", mystr)
['02:40PM', '12:29AM']
>>>

Also, you can shorten your Regex to \d\d:\d\d(?:A|P)M.

此外,您可以将 Regex 缩短为\d\d:\d\d(?:A|P)M.

回答by matt forsythe

Are you accidentally grabbing the 1st cluster (the stuff in that matches the portion of the pattern in the parentheses) instead of the "0st" cluster (which is the whole match)?

您是否不小心抓住了第一个集群(与括号中模式部分匹配的内容)而不是“第 0 个”集群(这是整个匹配项)?

回答by porges

It sounds like you're accessing group 1, when you need to be accessing group 0.

当您需要访问组 0 时,听起来您正在访问组 1。

The groups in your regex are as follows:

正则表达式中的组如下:

\d{2}:\d{2}(AM|PM)
           |-----|  - group 1
|----------------|  - group 0 (always the match of the entire pattern)

You can access the entire match via:

您可以通过以下方式访问整个比赛:

timePattern.match('02:40PM').group(0)