N
N
nuclear_kote2021-02-02 16:03:44
Java
nuclear_kote, 2021-02-02 16:03:44

How to get the values ​​of an array with a regular expression?

\\"bla\\":\[(?:\\"(\d+)\\",?)+\]


//\"bla\":[\"1000\",\"1002\",\"1\",\"22222\",\"333\",\"3333\",\"33344\"]

Matcher matcher = pattern.matcher(str);
if (matcher.find()) {
    System.out.println(matcher.group(1));
}


split do not offer

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
gsaw, 2021-02-02
@gsaw

I don't think it's possible. It is easier and clearer to do it in two steps, first extract the contents of
"bla" from the square brackets:\[([^\]]*)\]
And then from the found contents of the square brackets get all the groups
"([0-9]+)"

String input = "\"bla\":[\"1000\",\"1002\",\"1\",\"22222\",\"333\",\"3333\",\"33344\"]";
        Pattern pattern = Pattern.compile("([0-9]){1,}");
        Matcher matcher = pattern.matcher(input.replaceAll("\"bla\":\\[([^\\]])\\]", "$1"));
        while (matcher.find()) {
            System.out.println("g:"+ matcher.start() + " " + matcher.group());
        }

And if you do not advise split, then it's probably easier to use gson. There you can bypass the json structure and get an array without reinventing the wheel and not necessarily

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question