Arun Paul
Arun Paul

Reputation: 167

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.

Upvotes: 4

Views: 100

Answers (1)

Nidhin Joseph
Nidhin Joseph

Reputation: 1481

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

You may try this.

Upvotes: 5

Related Questions