0

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

3
  • thanks, I have correct that. but now it returns [("business", "day"), ("weeks")] without number. Commented Oct 18, 2016 at 17:30
  • Now if there's no business you require two whitespace characters. Commented Oct 18, 2016 at 17:30
  • you forgot to match characters between "business" & "12". Commented Oct 18, 2016 at 17:35

4 Answers 4

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

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?

Debuggex Demo

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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)

1 Comment

If you don't include any capture groups, then re.findall will return the entire match by default, so the parentheses are not necessary.
-2

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

3 Comments

code only answers are generally not as helpful as an explanation of how or why. Also, there is a formatter for code (the {} button)
this is not a code fragment, but rather a regex string to use to get desired output. I could have written "You need to tweak your regex to this:" but I thought it is rather implied.
which still doesn't explain why this is the right answer, or how the regex works (or, alternatively, why their regex doesn't). Those are important. Notice the accepted answer gives a solution, the reason why his approach doesn't work and a demo to test against. Normally I also do a breakdown, but Debuggex does an excellent job of doing a visual breakdown if you click through the link.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.