嗨,我想得到以下匹配:
test = re.compile(r' [0-12](am|pm) [1-1000] days from (yesterday|today|tomorrow)')
这场比赛:
print test.match(" 3pm 2 days from today")
它没有返回,我做错了什么?我正在进入正则表达式并阅读我认为应该工作的文档!任何帮助赞赏的圣诞节
我在NLP HERE中使用与上述类似的过程询问关于系统设计的新问题
这是我戴着戒指的帽子.仔细研究这个正则表达式将教几个教训:
import re
reobj = re.compile(
r"""# Loosely match a date/time reference
^ # Anchor to start of string.
\s* # Optional leading whitespace.
(?P<time> # $time: military or AM/PM time.
(?: # Group for military hours options.
[2][0-3] # Hour is either 20, 21, 22, 23,
| [01]?[0-9] # or 0-9, 00-09 or 10-19
) # End group of military hours options.
(?: # Group for optional minutes.
: # Hours and minutes separated by ":"
[0-5][0-9] # 00-59 minutes
)? # Military minutes are optional.
| # or time is given in AM/PM format.
(?:1[0-2]|0?[1-9]) # 1-12 or 01-12 AM/PM options (hour)
(?::[0-5][0-9])? # Optional minutes for AM/PM time.
\s* # Optional whitespace before AM/PM.
[ap]m # Required AM or PM (case insensitive)
) # End group of time options.
\s+ # Required whitespace.
(?P<offset> \d+ ) # $offset: count of time increments.
\s+ # Required whitespace.
(?P<units> # $units: units of time increment.
(?:sec(?:ond)?|min(ute)?|hour|day|week|month|year|decade|century)
s? # Time units may have optional plural "s".
) # End $units: units of time increment.
\s+ # Required whitespace.
(?P<dir>from|before|after|since) # #dir: Time offset direction.
\s+ # Required whitespace.
(?P<base>yesterday|today|tomorrow|(?:right )?now)
\s* # Optional whitespace before end.
$ # Anchor to end of string.""",
re.IGNORECASE | re.VERBOSE)
match = reobj.match(' 3 pm 2 days from today')
if match:
print('Time: %s' % (match.group('time')))
print('Offset: %s' % (match.group('offset')))
print('Units: %s' % (match.group('units')))
print('Direction: %s' % (match.group('dir')))
print('Base time: %s' % (match.group('base')))
else:
print("No match.")
Run Code Online (Sandbox Code Playgroud)
输出:
r"""
Time: 3 pm
Offset: 2
Units: days
Direction: from
Base time: today
"""
Run Code Online (Sandbox Code Playgroud)
这个正则表达式说明了一些可以吸取的教训:
现代正则表达式包含丰富而强大的语言.一旦你学习了语法并养成了编写冗长,正确缩进,注释良好的代码的习惯,那么即使是复杂的正则表达式也很容易编写,易于阅读且易于维护.不幸的是,他们因困难,笨拙和容易出错而声名鹊起(因此不适合复杂的任务).
快乐regexing!
| 归档时间: |
|
| 查看次数: |
3593 次 |
| 最近记录: |