How to remove parentheses around just one word per line

Say I have a line like this:

s = '((Xyz_lk) some stuff (XYZ_l)) (and even more stuff (XyZ))' 

I would like to remove parentheses only around individual words to get:

 '(Xyz_lk some stuff XYZ_l) (and even more stuff XyZ)' 

How do I do this in Python? So far I have been able to remove them along with the text using

 re.sub('\(\w+\)', '', s) 

which gives

 '( some stuff ) (and even more stuff )' 

How can I remove only brackets and save text inside them?

+7
python regex
source share
2 answers
 re.sub(r'\((\w+)\)',r'\1',s) 

Use \1 or feedback.

+15
source share

You can use the backlink (in fact, in the sub function they called the group link numbers):

 >>> s='((Xyz_lk) some stuff (XYZ_l)) (and even more stuff (XyZ))' >>> >>> re.sub(r'\((\w+)\)',r'\1',s) '(Xyz_lk some stuff XYZ_l) (and even more stuff XyZ)' >>> 

For more information, read the following explanation of backlinks from http://www.regular-expressions.info/backref.html :

Backreferences correspond to the same text that was previously matched by the capture group. Suppose you want to combine a pair of opening and closing HTML tags and text between them. By placing the opening tag in the backreference, we can reuse the tag name for the closing tag. Here's how: <([AZ][A-Z0-9]*)\b[^>]*>.*?</\1> . This regular expression contains only one pair of parentheses that hold the string matched against [AZ][A-Z0-9]* . This is the opening HTML tag. (Since HTML tags are case-insensitive, this regular expression requires case-insensitiveness.) Backlink \1 (backslash) refers to the first capture group. \1 matches the same text that was matched by the first capture group. / before it is a literal character. This is just a slash in the closing HTML tag that we are trying to match.

+5
source share

All Articles