Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RegEx remove parentheses from string

Tags:

regex

If I have:

string = (1000.00)

How can I use regex to remove the parentheses and get the output as 1000.00?

Thanks

like image 353
MohammedAli_ Avatar asked Apr 24 '12 09:04

MohammedAli_


People also ask

How do you remove parentheses from a string?

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.

How do you remove parentheses from a string in Python regex?

For using regex to remove parentheses from string in Python, we can use the re. sub() or pandas. str. replace() function.

What is difference [] and () in regex?

[] denotes a character class. () denotes a capturing group. [a-z0-9] -- One character that is in the range of a-z OR 0-9.

What does '$' mean in regex?

$ 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.


2 Answers

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.

like image 128
Aziz Shaikh Avatar answered Oct 08 '22 01:10

Aziz Shaikh


Replace the pattern:

\((.+?)\)

With the replacement pattern:

\1

Note the escaping of the parentheses () for their literal meaning as parenthesis characters.

like image 42
Li-aung Yip Avatar answered Oct 08 '22 02:10

Li-aung Yip