A
A
albertalexandrov2018-06-30 14:09:36
Python
albertalexandrov, 2018-06-30 14:09:36

What is the best way to find a date in a string?

Hello!
There was a task among users whose data is given by the VKontakte API, to find those who have the full date of birth. User list example:

users = [{'id': 6477, 'first_name': 'Oksana', 'last_name': 'Ozernaya'}, 
{'id': 6477, 'first_name': 'Oksana', 'last_name': 'Ozernaya', 'bdate': '5.3'}, 
{'id': 96163, 'first_name': 'Sergey', 'last_name': 'Kushlevich', 'bdate': '5.3.1986'}, ...]

Dates can be in three formats: no key, day-month, day-month-year.
Now I do this:
for user in users:
    if 'bdate' in user and re.match(r'^\d{1,2}\.\d{1,2}\.\d{4}$', friend['bdate']):
        age = 2018 - int(user['bdate'].split('.')[-1])

Maybe it can be done easier/better/optimized?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question