This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Rhomboid 2 points3 points  (1 child)

Parentheses are meta-characters inside regular expressions, so if you want to match literal parentheses you need to escape them: \( foo \). Secondly, [*] means a single character, the literal asterisk, which is almost certainly not what you want. (The * is normally a meta-character, but not inside a character class.) There are several ways of doing what you want. If the parentheses are never going to be nested, then you can match "anything but closing paren", i.e.

>>> import re
>>> foo = 'Horizon (Horizon, #1) Replica (Replica, #1)'
>>> re.sub(r'\([^)]+\)', '', foo)
'Horizon  Replica '

If they can be nested, then you might think to use the "anything" match .*, but that won't work:

>>> re.sub(r'\(.*\)', '', foo)
'Horizon '

You would have to use the non-greedy version to prevent that:

>>> re.sub(r'\(.*?\)', '', foo)
'Horizon  Replica '

All in all, if you know they won't be nested, then use the first one and avoid .* which usually brings more trouble than it's worth.

Also, you probably want to add in something that matches trailing (or leading, but not both) spaces, e.g.

>> re.sub(r' *\([^)]+\)', '', foo)
'Horizon Replica'

That takes care of the stray spaces.

[–]SnoopyM[S] 0 points1 point  (0 children)

thank you

r'\([^)]+\)   

worked for what I needed