-3

I have a list:

rand_strings = [
        "Hi 06/02/2020 my name is Arjun", 
        "Oh 2992 that's nice", 
        "Some other string 2019-05-12",
]

I want to remove only the dates of these two formats from them. Not all recognized dates. This is my desired output:

rand_strings = [
        "Hi my name is Arjun", 
        "Oh 2992 that's nice", 
        "Some other string " #whitespace doesn't matter
]

The list is thousands of elements long and several of them have these dates to be removed.

1 Answers1

0

Use regex expressions to match exactly those 2 types:

import re

rand_strings = [
        "Hi 06/02/2020 my name is Arjun", 
        "Oh 2992 that's nice", 
        "Some other string 2019-05-12",
]

rand_strings = [re.sub(r"(\d{4}\-\d{2}\-\d{2}|\d{2}/\d{2}/\d{4}", "", s) for s in rand_strings]

You can then remove extra whitespace characters.

M Z
  • 4,571
  • 2
  • 13
  • 27