Home > Blockchain >  regular expression to filter out dates from a list
regular expression to filter out dates from a list

Time:04-02

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) ]

CodePudding user response:

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)]
  • Related