If you insist using regex than you can use this [^/\\s]+
with Pattern like this :
String[] strs = new String[]{"/pathvalue1/path-value-one",
"/pathvalue2/path-value",
"/pathvalue3/pathvaluethree"};
String regex = "[^/\\s]+";
for (String str : strs) {
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
Outputs
pathvalue1
path-value-one
pathvalue2
path-value
pathvalue3
pathvaluethree
regex demo
You can solve your problem also using split just one point if you use split str.split("/")
directly than you can get this results :
[, pathvalue1, path-value-one]
To make sure that you get the correct result, then replace the no usefull / or spaces in the start of the url so you can use :
String[] strs = new String[]{"/pathvalue1/path-value-one",
"/pathvalue2/path-value",
"/pathvalue3/pathvaluethree"};
for (String str : strs) {
System.out.println(Arrays.toString(str.replaceAll("^[/\\s]", "").split("/")));
}
Outputs
[pathvalue1, path-value-one]
[pathvalue2, path-value]
[pathvalue3, pathvaluethree]