3

I was learning python regex and wondering how to extract numbers from x days y hours z minutes?

Note: there are no months or seconds, only one or more of days, minutes and seconds are allowed.

My attempt

import re s1 = '5 days 19 hours 30 minutes' s2 = '5 days' s3 = '19 hours' s4 = '5 days 19 hours' pat = r'((\d+)(?<=\sdays))?((\d+)(?<=\shours))?((\d+)(?<=\sminutes))?' d,h,m = re.findall(pat,s) Note: 2 days 3 hours ==> d=2 h=3 2 hours 3 minutes ==> h=2 m=3 

I am struggling to fix lookbehinds. How to fix the problem?

1 Answer 1

4

why you add ?<= ? Look, I add groups to your regex and add missing space separations

Then you can match for your regex and select groups.

Python 3.7

import re s4 = '5 days 19 hours' pat = r'(?P<days>(\d+)(\sdays))? ?(?P<hours>(\d+)(\shours))? ?(?P<minutes>(\d+)(\sminutes))?' match = re.match(pat, s4) if match: print(match.groupdict()) # print all groups # Output: {'days': '5 days', 'hours': '19 hours', 'minutes': None} 

If you only want to match the number of the values, instead the name and the number, you need to use the next pattern:

r'((?P<days>\d+) days)? ?((?P<hours>\d+) hours)? ?((?P<minutes>\d+) minutes)?' """ Here I deconstruct the pattern, then you can look at it and the next time you can make your own without help. ((?P<days>\d+) days)? Match numbers + space + "days" ? Match space ((?P<hours>\d+) hours)? Match numbers + space + "hours" ? Match space ((?P<minutes>\d+) minutes)? Match numbers + space + "minutes" If you want the group "days" return you the number and the word "days" yo need to use it as: (?P<days>\d+ days) """ 

https://regex101.com/ is a good place to try your patterns. It has a good IDE that helps you to understand what each element do.

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

2 Comments

the walrus operator of python 3.8 allows you to avoid doing a = True; if a: print(a). With it you can do if a:= True: print(a). You can declare a variable inside a conditional! There's a good guide: realpython.com/lessons/assignment-expressions
Thanks a lot, is there a way to just extract numbers? eg. match['days'] = 5 instead of '5 days'

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.