Home > Mobile >  How to find last group between "/" from the back
How to find last group between "/" from the back

Time:06-29

Please help with java regex.

How to find last group between / from the back?

Example:

sdfsdf/inb/SDF/sdf1/int

answer: sdf1

CodePudding user response:

We could use String#replaceAll for a one-liner solution:

String input = "sdfsdf/inb/SDF/sdf1/int";
String output = input.replaceAll(".*/([^/] )/[^/] $", "$1");
System.out.println(output);  // sdf1

CodePudding user response:

You could do this:

    String s = "sdfsdf/inb/SDF/sdf1/int";

    String[] array = s.split("/");

    String result = array[array.length - 2];

    System.out.println(result);
  • Related