javaregex

java regex get value after specific value


I have some endpoints that contains /bot/{idBot}, i need to extract that idBot.

Can we do it via regex in the java ?

here are some examples of endpoints:

/bot/6/block/30/content/text
/bot/6/block/content/input/list/option/30
/account/bot/32/language
/account/bot/6

tried with that with no sucess!

Matcher m2 = Pattern.compile("/bot/(\\d+)").matcher(path);

Edit: I have some endpoints like this:

/account/checkToken
/account/checkUser

those need to be ignored!

thanks!!


Solution

  • You could use String#replaceAll for a regex one-liner option:

    String input = "/account/bot/32/language";
    String num = input.matches(".*/bot/\\d+.*") ? input.replaceAll(".*/bot/(\\d+).*", "$1") : "";
    System.out.println(num);