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 -

reflection - How to access the object-members of an object declaration in kotlin -

php - Doctrine Query Builder Error on Join: [Syntax Error] line 0, col 87: Error: Expected Literal, got 'JOIN' -