Computer >> Computer tutorials >  >> Programming >> Python

How to match parentheses in Python regular expression?


The following code matches parentheses in the string s  and then removes the parentheses in string s1 using Python regular expression.

Example

import re
s = 'I love book()'
result = re.search(r'\(\)',s)
print result.group()
s1 = 'I love book(s)'
result2 = re.sub(r'[\(\)]','',s1)
print result2

Output

This gives the output

()
I love books