jhagege
jhagege

Reputation: 1586

Remove datetime from filename created with strftime

I have files with filenames such as:

"001_-_h_-_hydrogen.2014-09-13.14-27-45.jpg"

I would like to remove the datetime created with strftime from the filename and get:

"001_-_h_-_hydrogen.jpg"

The function would look like:

def remove_datetime(str): 

    return str_withoutdatetime

I'm really new to Regexes. How could I do that in Python?

Upvotes: 0

Views: 796

Answers (2)

nu11p01n73R
nu11p01n73R

Reputation: 26667

You can use a regex of the form

(\.\d{2,4}(-\d{2}){2}){2}

Example usage

>>> str="001_-_h_-_hydrogen.2014-09-13.14-27-45.jpg"
>>> re.sub(r'(\.\d{2,4}(-\d{2}){2}){2}', '', str)
'001_-_h_-_hydrogen.jpg'

OR

to be more specific

>>> re.sub(r'(\.\d{2,4}(-\d{2}){2}){2}(?=.jpg)', '', str)
'001_-_h_-_hydrogen.jpg'

What it does?

  • \. matches a . here it matches the dot after hydrogen

  • \d{2,4} matches minimum of 2 (for hour) and maximum 4 (for year) digits

  • (-\d{2}) matches digits 2 times that is month and minute

  • {2} quantifies two times, that is again it goes for a match for date/ or second

  • {2} quantifies the match again for the entire tiem

  • (?=.jpg) checks if the matched string is followed by .jpg

You can also write a less cryptic else long regex like

\.\d{4}(-\d{2}){2}\.\d{2}(-\d{2}){2}(?=.jpg)

which would do the same purpose as

>>> re.sub(r'.\d{4}(-\d{2}){2}\.\d{2}(-\d{2}){2}(?=.jpg)', '', str)
'001_-_h_-_hydrogen.jpg'

Upvotes: 1

Dinesh
Dinesh

Reputation: 4559

>>> x="001_-_h_-_hydrogen.2014-09-13.14-27-45.jpg"
>>> xnew= re.sub(r'\.\d{4}-\d{2}-\d{2}\.\d{2}-\d{2}-\d{2}', '', x)
>>> xnew
'001_-_h_-_hydrogen.jpg'

See also: Python re

Upvotes: 0

Related Questions