[Solved] Get a specific value from a string in Java [closed]


Depends on what you really want to do, for example if you only want to get the value of a given key, given that key and value are separated by colon and key/value are separated by comma, you can just split in this way string first by comma and then by colon :

String[] couple = string.split(",");

 for(int i =0; i < couple.length ; i++) {
    String[] items =couple[i].split(":");
    items[0]; //Key
    items[1]; //Value
}

If you want to do it with a regular expression you can do in this way :

    String string = "Name:armand,Age:22,";
    Pattern pattern = Pattern.compile("(\\w+?):(\\w+?),");
    Matcher matcher = pattern.matcher(string);
    while (matcher.find()) {
        String key = matcher.group(1);
        String value = matcher.group(2);

        System.out.println("Key : " + key + " value : " + value);
    }

1

solved Get a specific value from a string in Java [closed]