How to break this line using regex in Java?

From this line: "/resources/pages/id/AirOceanFreight.xhtml"

I need to get two substrings: a line after pages / and a line before .xhtml.

/ resources / pages / is persistent. id and AirOceanFreight.

Any help appreciated, thanks!

+3
source share
4 answers

I like Jakarta Commons Lang StringUtils :

String x = StringUtils.substringBetween(string, "/resources/pages/", ".xhtml");

Or, if ".xhtml"it can also be displayed in the middle of the line:

String x = substringBeforeLast(
              substringAfter(string, "/resources/pages/"), ".xhtml");

(I also like static imports)

+9
source

An alternative without jakarta. Using constants:

 private final static String PATH = "/resources/pages/";
 private final static String EXT = ".xhtml";

you just need to do:

 String result = filename.substring(PATH.length(), filename.lastIndexOf(EXT));
+3
source

split .

 String test = "/resources/pages/id/AirOceanFreight.xhtml";
 String result = test.split(".xhtml")[0].split("/resources/pages/")[1];
+1

:

(<? = \/) (.?+. (= XHTML))

0
source

Source: https://habr.com/ru/post/1768589/


All Articles