正则表达式匹配Python中的几个字符串

时间:2016-10-18 17:24:19

标签: python regex

我对Python中的正则表达式有疑问。

可能的变化可能是

10小时,12周或7个工作日。

我想让我的正则表达式像

string = "I have an 7 business day trip and 12 weeks vacation."
re.findall(r'\d+\s(business)?\s(hours|weeks|days)', string)

所以我希望找到" 7个工作日"和" 12周"但它返回无

4 个答案:

答案 0 :(得分:2)

string = "I have an 7 business day trip and 12 weeks vacation."
print re.findall(r'\d+\s(?:business\s)?(?:hour|week|day)s?', string)
['7 business day', '12 weeks']

\d+\s(?:business\s)?(?:hour|week|day)s?

Debuggex Demo

演示应该解释这是如何工作的。你的原因不是因为它正在寻找不匹配的7 businessdays

虽然如果您不想接受business week/hour,则需要进一步修改:

\d+\s(?:hour|week|(?:business )?day)s?

Debuggex Demo

答案 1 :(得分:1)

你需要调整你的正则表达式:

>>> string = "I have an 7 business day trip and 12 weeks vacation."
>>> print re.findall(r'(\d+)\s*(?:business day|hour|week)s?', string)
['7', '12']

这与最后跟business dayhourweek以及可选s的任何数字相匹配。

答案 2 :(得分:0)

与@ anubhava的答案相似,但匹配" 7个工作日"而不只是" 7"。只需将右括号从\ d +后移到结尾:

re.findall(r'(\d+\s*(?:business day|hour|week)s?)', string)

答案 3 :(得分:-2)

\ d + \ S +(商业\ S)(小时|一周|天)??的