-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?

3
  • You're missing a find() call and verification. Commented Jun 8, 2016 at 15:52
  • You need to use the replaceAll() method in the right way :) Commented Jun 8, 2016 at 15:57
  • this is not the right approach use a suffix and substring, don't over use regexpr s.substring(0, s.length() - suffix.length()) Commented Jun 8, 2016 at 16:01

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.