-1

Let's say I have a program which has inputs like this

string28something8words2000

I want to fetch that date embedded it the string so I can get:

day = 28
month = 8
year = 2000

How can I implement this using regular expressions?

The month 8 can not be explicitly a number but also written as "august".

This is not the case of the marked as duplicate which has no optionals.

If I get a list containing:

string28somethingaugustwords2000

With that solution I get only [28, 2000] and if the number is equal or minor to 12 there is no way to know if it was a month or a day.

JulianP
  • 97
  • 1
  • 11

1 Answers1

5

You can use re.findall:

s = 'string28something8words2000'
day, month, year = map(int, re.findall('\d+', s))
Ajax1234
  • 69,937
  • 8
  • 61
  • 102