If I have:
string = (1000.00)
How can I use regex to remove the parentheses and get the output as 1000.00
?
Thanks
sub() function to remove parentheses from a string. We removed parentheses from our string variable using the re. sub() function in the code above. We achieved our goal by replacing the opening and closing parentheses with an empty string and storing the return value inside our original string.
For using regex to remove parentheses from string in Python, we can use the re. sub() or pandas. str. replace() function.
[] denotes a character class. () denotes a capturing group. [a-z0-9] -- One character that is in the range of a-z OR 0-9.
$ means "Match the end of the string" (the position after the last character in the string). Both are called anchors and ensure that the entire string is matched instead of just a substring.
Try this regular expression:
s/([()])//g
Brief explanation:
[]
is used to create a character set for any regular expression. My character set for this particular case is composed of (
and )
. So overall, substitute (
and )
with an empty string.
Replace the pattern:
\((.+?)\)
With the replacement pattern:
\1
Note the escaping of the parentheses ()
for their literal meaning as parenthesis characters.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With