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' -- https://mail.python.org/mailman/listinfo/python-list