开发者

RegEx, select anything thats not in brackets

开发者 https://www.devze.com 2022-12-30 19:28 出处:网络
In RegEx, how would I select anything t开发者_JAVA百科hats not in brackets: E.g. Xxxxxxx (01010101) would return Xxxxxxx ?

In RegEx, how would I select anything t开发者_JAVA百科hats not in brackets:

E.g.

Xxxxxxx (01010101) would return Xxxxxxx ?

Thanks!


For the existing sample, this will do:

(.+) \(


In Python:

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

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


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"
0

精彩评论

暂无评论...
验证码 换一张
取 消