1

I have a string having key and value:

A=1,B=2,C=3,D=4,E=5

I need a split regex in java to get these values (1,2,3,4,5) from above string.

3 Answers 3

1

You can use regex with this pattern [0-9]+

Example:

    String toIndex = "A=1,B=2,C=3,D=4,E=5";
    Pattern p = Pattern.compile("[0-9]+");
    Matcher m = p.matcher(toIndex);
    while (m.find()) {
        System.out.println(m.group());
    }

and in the while instead of printing the groups add then to a list or similar for latter manipulation

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Xoce, There may be string in place of integer. So what regex i should use. A=AA,B=BB,C=CC,D=DD,E=EE
1

use this:

    String s = "A=1,B=2,C=3,D=4,E=5";
    Pattern p = Pattern.compile("=(\\d+)");

    Matcher m = p.matcher(s);
    while (m.find()) {
        System.out.println(m.group(1));
    }

\d+ is for one until n digits

4 Comments

Thanks @Jens, There may be string in place of integer. So what regex i should use. Fg: A=AA,B=BB,C=CC,D=DD,E=EE
@VarunArya use \\w+ instead of \\d+
Yup (y). One more question why Xoce rate it to negetive??
@VarunArya I do not know. ask him
0

Since you asked to use split, this will work as well.

    String str2 = "A=1,B=2,C=3,D=4,E=5";
    String [] between = str2.split("=+|,");
    for(int i=1; i<between.length; i+=2){
        System.out.println(between[i]);
    }

Works for both strings and numbers between = and ,

(A=AA,B=BB,C=CC,D=DD,E=EE)

2 Comments

That is the one. I want.. Thanks a lot @jsurf :)
Anytime @VarunArya

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.