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?
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
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With