RegEx, select anything not shown in parentheses

In RegEx, how would I select something that is not in brackets:

eg.

Xxxxxxx (01010101)will return Xxxxxxx?

Thank!

+3
source share
3 answers

For an existing sample, this will do:

(.+) \(
+2
source

Use \([^)]*\)as a delimiter, either in split, or java.util.Scanner, etc., or just use it to replace with "".

In Java:

    System.out.println(Arrays.toString(
        "abc(xyz)def(123)".split("\\([^)]*\\)"))
    ); // prints "[abc, def]"
    System.out.println(
        "abc(xyz)def(123)".replaceAll("\\([^)]*\\)", "")
    ); // prints "abcdef"
+1
source

In Python:

import re
def removeparens(inputstring):
    return re.sub(r"\([^)]*\)", "", inputstring)

will provide this functionality provided that parens are never nested.

0
source

Source: https://habr.com/ru/post/1746358/


All Articles