I have a list of strings
my_strings = [
"2002-03-04 with Matt",
"Important: 2016-01-23 with Mary",
"with Tom on 2015-06-30",
]
I want to extract:
- date (always in yyyy-mm-dd format)
- person (always with person), but I don’t want a “c”
I could do:
import re
pattern = r'.*(\d{4}-\d{2}-\d{2}).*with \b([^\b]+)\b.*'
matched = [re.match(pattern, x).groups() for x in my_strings]
but it fails because the template does not match "with Tom on 2015-06-30".
Questions
How to specify a regular expression pattern to be indifferent to the order in which the date or face is displayed in a string?
and
How to ensure that a method groups()returns them in the same order every time?
I expect the result to look like this:
[('2002-03-04', 'Matt'), ('2016-01-23', 'Mary'), ('2015-06-30', 'Tom')]
source
share