Reputation: 1586
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
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