regex - remove the last [blablabla] or (blabla) including [] and () with python -
this question has answer here:
- remove text between () , [] in python 4 answers
- how can remove text within parentheses regex? 8 answers
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
Post a Comment