Lzypenguin
Lzypenguin

Reputation: 955

How can I remove the date and time from a string IF it contains it?

I have a list of strings. Some of them contain a date and timestamp at the end, but most don't. The date/time format will always be the same format.

sampleString = ['String 1', 'String2', 'String 3 05.24.2019 04:48:24']

I am looking for a statement like this:

if 'MM.DD.YYYY' in sampleString:
    samplestring = samplestring.split('MM.DD.YYYY')[0]

print(sampleString)
>>>['String 1', 'String2', 'String 3']

But that is clearly not working. Any help is appreciated.

Upvotes: 0

Views: 356

Answers (4)

ferhatelmas
ferhatelmas

Reputation: 3978

import re

input = ['String 1', 'String2', 'String 3 05.24.2019 04:48:24']

# compile our regex
r = re.compile(r'(.+) \d{2}\.\d{2}\.\d{4} \d{2}:\d{2}:\d{2}$')

# take first part
output = [r.sub(r'\1', s) for s in ls]

print(ls)
# ['String 1', 'String2', 'String 3']

clarification for regex:

  • \d: matches any number
  • \.: literal dot
  • {n}: repeat the previous token exactly n times
  • $: matches at the end of the string
  • :: literal colon

Upvotes: 2

Rakesh
Rakesh

Reputation: 82765

Looks like you can use the search function from the re module

import re
data = ['String 1', 'String2', 'String 3 05.24.2019 04:48:24']
pattern = re.compile(r"\b\d{2}\.\d{2}\.\d{4}\b")
print([pattern.split(i)[0].strip() if pattern.search(i) else i for i in data])

Output:

['String 1', 'String2', 'String 3']

Upvotes: 1

Alex 75
Alex 75

Reputation: 3236

Just another solution, without a compiled regex and with a clear regex syntax.
It is less generic, it works only with you precise input, a string followed by a space and the date-time format as specified.

import re

def remove_date(s): 
    if re.search("\d\d:\d\d:\d\d$", s): return s[:-20]  // remove the last 20 characters
    else: return s

sampleString = ['String 1', 'String2', 'String 3 05.24.2019 04:48:24'] 
sampleString = list(map(remove_date, sampleString))

print(sampleString)

What is re ?
To play with regex: https://regexr.com/

Upvotes: 0

Filip Młynarski
Filip Młynarski

Reputation: 3612

import re

def filter_date(date):
    match = re.search(r'\d{2}.\d{2}.\d{4} \d{2}:\d{2}:\d{2}', date)
    return date.replace(match.group(), '').strip() if match else date

xs = ['String 1', 'String2', 'String 3 05.24.2019 04:48:24']
out = list(map(filter_date, xs))

print(out)

Output:

['String 1', 'String2', 'String 3']

Upvotes: 0

Related Questions