I want to remove Digits with Parentheses () from string using regex in Python
Example : Hello World(4353)
Output: Hello World
Example : Hello World(ABC)
Output : Hello World(ABC)
I tried this reg but not working Perfectly...
s = "Satbaulia Khurd(159ds)"
# pattern=r"([\d ]*(\(\d \))?[\d ])"
pattern= r'\([^()]*\)'
res = re.sub(pattern, "", s)
print(res)
Output:
Satbaulia Khurd
CodePudding user response:
As suggested by Quang Hoang in comments '\(\d \)'
is a valid regex. That is the code I written based on your examples.
import re
string = "Hello World(12345)"
pattern = re.sub(r'\(\d \)', '', string)
# Hello World(ABC) -> Hello World(ABC)
print(pattern)