How to match parentheses in Python regular expression?

PythonServer Side ProgrammingProgramming

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

raja
Published on 10-Jan-2018 13:04:49
Advertisements