regex - remove the last [blablabla] or (blabla) including [] and () with python -


i have string in form:

sdp_123_cc[comments]  

i want remove square brackets , content inside of them end of string. output be:

sdp_123_cc 

another example string:

sdp_xx_yy(123)_zz(xxxxx)  

i want remove parenthesis , content inside of them end of string. output be:

sdp_xx_yy(123)_zz 

basically, want remove last square brackets or parenthesis in string, , content in-between them. how can accomplish this?

in case there digits or characters inside parenthesis or brackets use:

>>> import re >>> s = 'sdp_xx_yy(123)_zz(xxxxx)'  >>> re.sub(r'(?:\(\w*\)|\[\w*\])$', '', s) 'sdp_xx_yy(123)_zz'  >>> s = 'sdp_123_cc[comments]' >>> re.sub(r'(?:\(\w*\)|\[\w*\])$', '', s) 'sdp_123_cc' 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -