Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression for Separating Paths [duplicate]

Tags:

java

regex

Hello I was trying to use Java regular expression to get the required context path from the following path information.

String path = "/Systems/lenovo/";

I want to write regular expression to get "/Systems" and "/lenovo" separately.

I tried the following regular expression using groups but not working as expected.

String systemString = path.replaceAll("(.*)(/\\w+)([/][\\w+])", "$2") - to get "/Systems" - not working

String lenovoString = path.replaceAll("(.*)(/\\w+)([/][\\w+])", "$3") - to get "/lenovo" - working.

Could any body tell me what might be the wrong in my Regx?

like image 467
M.S.Naidu Avatar asked Dec 18 '15 09:12

M.S.Naidu


3 Answers

You can try

String PATH_SEPARATOR = "/"; 
String str = "/Systems/lenovo/";
String[] res = str.substring(1).split(PATH_SEPARATOR);

IDEONE DEMO

And if you want to retain the / before the string then you can simply add it like:

"/"+res[0]

IDEONE DEMO

like image 194
Rahul Tripathi Avatar answered Nov 13 '22 14:11

Rahul Tripathi


Just split like this:

String[] parts = path.replaceAll("/$", "").split("(?=/)");

The replaceAll() call is to remove the trailing slash (if any).

See live demo of

String path = "/Systems/lenovo/";
String[] parts = path.replaceAll("/$", "").split("(?=/)");
Arrays.stream(parts).forEach(System.out::println);

producing

/Systems
/lenovo
like image 2
Bohemian Avatar answered Nov 13 '22 15:11

Bohemian


You shouldn't use this replaceAll with groups ($3) approach to get what you want.

What happens behind the scene with your approach is:

regex (.*)(/\\w+)([/][\\w+]) matches the string /Systems/l

Your expression is divided into the following groups:

$1 => (.*)
$2 => (/\\w+)
$3 => ([/][\\w+])

Each group matched the following part of your matched string /Systems/l

$1 => ''
$2 => /Systems
$3 => /l

So when you do

path.replaceAll("(.*)(/\\w+)([/][\\w+])", "$3")

you are essentially doing

'/Systems/lenovo/'.replaceAll(`/Systems/l`, '/l') => '/lenovo'

And when you use $2

'/Systems/lenovo/'.replaceAll(`/Systems/l`, '/Systems') => '/Systemsenovo/'

So it doesn't really make sense to use regex groups for this task and better use simple String.split method as others suggested on this page

like image 1
jonasnas Avatar answered Nov 13 '22 16:11

jonasnas