java regular expression find and replace

后端 未结 7 2125
执念已碎
执念已碎 2020-12-14 21:37

I am trying to find environment variables in input and replace them with values.

The pattern of env variable is ${\\\\.}

Pattern myPatte         


        
7条回答
  •  -上瘾入骨i
    2020-12-14 22:01

    Use groups once it is matched ${env1} will be your first group and then you use regex to replace what is in each group.

    Pattern p = Pattern.compile("(${\\.})");
    Matcher m = p.matcher(line);
    while (m.find())
      for (int j = 0; j <= m.groupCount(); j++)
        //here you do replacement - check on the net how to do it;)
    

提交回复
热议问题