How to Remove Text Within Parentheses With a Regex

How can I remove text within parentheses with a regex?

s/\([^)]*\)//

So in Python, you'd do:

re.sub(r'\([^)]*\)', '', filename)

JavaScript/regex: Remove text between parentheses

"Hello, this is Mike (example)".replace(/ *\([^)]*\) */g, "");

Result:

"Hello, this is Mike"

RegEx to remove text not inside parentheses

Use this:

.*?(\([^)]*\))

and replace with $1

Demo & explanation

How can I remove text within multi layer of parentheses python

With the re module (replace the innermost parenthesis until there's no more replacement to do):

import re

s = r'Sainte Anne -(Data with in (Boo) And good luck) Charenton'

nb_rep = 1

while (nb_rep):
(s, nb_rep) = re.subn(r'\([^()]*\)', '', s)

print(s)

With the regex module that allows recursion:

import regex

s = r'Sainte Anne -(Data with in (Boo) And good luck) Charenton'

print(regex.sub(r'\([^()]*+(?:(?R)[^()]*)*+\)', '', s))

Where (?R) refers to the whole pattern itself.



Related Topics



Leave a reply



Submit