Search code examples
python-3.xpython-re

How to print text between two round brackets ()


I was trying to print text in between round brackets but I ended up with entire text.

Here is an what I have used.

import re

s = '₹ 24.00 (8%)'
result = re.search('((.*))', s)
print(result.group(2))

output:

₹ 24.00 (8%)

Expected output:

8%

Solution

  • This should do the trick

    import re
    
    s = '₹ 24.00 (8%)'
    result = re.search('\((.*)\)', s)
    print(result.group(1))
    # 8%
    

    You need to escape the brackets if they are literal.