W
W
Wana12022-01-05 15:59:12
Java
Wana1, 2022-01-05 15:59:12

How to parse a variable with text?

There is some text, example
"TOKKEN=YVJ678RDH; NAME=YOUT;" it is necessary to output only NAME to remain in the variable.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Roo, 2022-01-05
@xez

Best with regular expressions and groups:

public static void main(String[] args) {
        var pattern = Pattern.compile("TOKKEN=(?<tokken>\\w+); NAME=(?<name>\\w+);");
        var string = "TOKKEN=YVJ678RDH; NAME=YOUT;";

        var matcher = pattern.matcher(string);

        if (matcher.find()) {

            var tokken = matcher.group("tokken");
            var name = matcher.group("name");

            System.out.println(tokken);
            System.out.println(name);
        }
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question