x89
x89

Reputation: 3490

regular expression to filter out dates from a list

I have a list that looks like this:

['','2022-03-31', 'euss', 'projects','2021-03-31']

I want to write a regular expression such that I delete all other items from the list and only keep those that have a date format. For example, 2022-03-31 and 2021-03-31.

I tried this but it doesn't seem to make a difference when I print my list out:

my_list = [re.sub(r"(\d+)/(\d+)/(\d+)",r"\3-\1-\2",i) for i in list(my_list) ]

Upvotes: 1

Views: 548

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627607

You can first check if there regex matches the string:

result = []
rx = re.compile(r'(\d+)-(\d+)-(\d+)')
for i in my_list:
    if rx.search(i):                          # Check if the regex matches the string
        result.append(rx.sub(r"\3-\1-\2", i)) # Add modified string to resulting list

See the Python demo. Output:

['31-2022-03', '31-2021-03']

You may write it as:

rx = re.compile(r'(\d+)-(\d+)-(\d+)')
my_list = [rx.sub(r"\3-\1-\2", i) for i in my_list if rx.search(i)]

Upvotes: 1

Related Questions