CafeHey
CafeHey

Reputation: 5800

RegEx, select anything thats not in brackets

In RegEx, how would I select anything thats not in brackets:

E.g.

Xxxxxxx (01010101) would return Xxxxxxx ?

Thanks!

Upvotes: 4

Views: 4714

Answers (3)

polygenelubricants
polygenelubricants

Reputation: 383746

Use \([^)]*\) as a delimiter, either in split, or a 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"

Upvotes: 1

Tim Pietzcker
Tim Pietzcker

Reputation: 336148

In Python:

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

will provide this functionality under the condition that parens are never nested.

Upvotes: 1

SilentGhost
SilentGhost

Reputation: 319601

For the existing sample, this will do:

(.+) \(

Upvotes: 3

Related Questions