简体   繁体   中英

Regular Expression to remove specific patterns

String str=",Name=Tom,Age=23,something=something,roll=1,somethng=55,"

I want to remove all those key value pairs from the string whose value is a number.

Now i am doing something like this

    Pattern p = Pattern.compile(",[^=]*?=([^,]*),");
    Matcher m = p.matcher(str);
    String result = "";
    while (m.find()) {
        if (!isNumeric(m.group(1))) {
            result += m.group(0);
        }

    }
    System.out.println(result);

The expected output is

",Name=Tom,something=something,"

But now i am getting

",Name=Tom,,something=something,"

Please help.

result=str.replaceAll(",[^=]*?=[0-9]+", "");
System.out.println(result);

You may try this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM