I have a list of strings with varying formats. Some of them start with a hyphen separated date time followed by a string that's a mixture of 16 numbers of letters. I would like to filter for only the strings that match this format. I've provided input and out put examples below. I'm not a regex expert, could someone please suggest a slick way to do this with python?
Input:
example_list=['2022-05-05-16-59-25-5840ZQ37F231D95W',
'wereD/22fdas/',
'mnkljlj/124kljf/oaahreljah',
'2022-09-11-16-59-25-5840XY37F231D95Z']
output:
['2022-05-05-16-59-25-5840ZQ37F231D95W',
'2022-09-11-16-59-25-5840XY37F231D95Z']
update:
using the suggestion below with re.match and list comprehension worked fine, thanks!
import re
[x for x in example_list if re.match("^\d{4}(-\d\d){5}-[A-Z\d]{16}$",x)]
CodePudding user response:
Try this:
^\d{4}(-\d\d){5}-[A-Z\d]{16}$
See live demo.
Regex breakdown:
^
start of input\d{4}
4 digits(-\d\d){5}
5 lots of a dash then 2 digits[A-Z\d]{16}
16 of a caps letter or a digit$
end of input