I have questions with regex in Python.
Possible variations can be
10 hours, 12 weeks or 7 business days.
I want to have my regex something like
string = "I have an 7 business day trip and 12 weeks vacation."
re.findall(r'\d+\s(business)?\s(hours|weeks|days)', string)
so that I expect to find "7 business day" and "12 weeks" but it returns None
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?
The demo should explain how this works. The reason yours wasn't is because it was looking for 7 businessdays
which doesn't match.
Although if you don't want to accept business week/hour
, you'll need to modify it further:
\d+\s(?:hour|week|(?:business )?day)s?
You need to tweak your regex to this:
>>> 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']
This matches any number that is followed by business day
or hour
or week
and an optional s
in the end.
Similar to @anubhava's answer but matches "7 business day" rather than just "7". Just move the closing parenthesis from after \\d+ to the end:
re.findall(r'(\d+\s*(?:business day|hour|week)s?)', string)
\\ d + \\ s +(业务)?(小时|周|天)s?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.