Regular expression

Peter Otten __peter__ at web.de
Wed Jul 26 08:13:23 EDT 2017


Kunal Jamdade wrote:

> There is a filename say:- 'first-324-True-rms-kjhg-Meterc639.html' .
> 
> I want to extract the last 4 characters. I tried different regex. but i am
> not getting it right.
> 
> Can anyone suggest me how should i proceed.?

You don't need a regular expression:

>>> import os
>>> name = 'first-324-True-rms-kjhg-Meterc639.html'
>>> bname, ext = os.path.splitext(name)
>>> bname
'first-324-True-rms-kjhg-Meterc639'
>>> ext
'.html'
>>> bname[-4:]
'c639'
>>> bname[:-4]
'first-324-True-rms-kjhg-Meter'
>>> bname[:-4] + ext
'first-324-True-rms-kjhg-Meter.html'

Canned into a function:

>>> def strip_last_four(filename):
...     path, name = os.path.split(filename)
...     bname, ext = os.path.splitext(name)
...     return os.path.join(path, bname[:-4] + ext)
... 
>>> strip_last_four("/foo/bar/baz1234.html")
'/foo/bar/baz.html'
>>> strip_last_four("/foo/bar/1234.html")
'/foo/bar/.html'
>>> strip_last_four("/foo/bar/bar.html")
'/foo/bar/.html'





More information about the Python-list mailing list