-2

I have to extract the value of an environment variable using a java method.

My path is ${rootPath}/user/settings and the value I want to get is rootPath.

I tried the following but it says "not match found":

Pattern.compile("\\$\\{(\\w+)\\}").matcher("${rootPath}/user/settings").group(1);

If I use the replace method it replaces the ${rootPath} value. What am I doing wrong?

mannuk
  • 1,259
  • 5
  • 21
  • 43

1 Answers1

1

Using replaceAll() would not be a bad idea IMHO and the code would look quite simple :):

public static void main(String[] args) {
    String s = "${rootPath}/user/settings";
    System.out.println(s.replaceAll("\\$\\{(.*?)\\}.*","$1"));
}

O/P :

rootPath

TheLostMind
  • 35,966
  • 12
  • 68
  • 104