Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用正则表达式从Java中的path提取环境变量名_Java_Regex - Fatal编程技术网

使用正则表达式从Java中的path提取环境变量名

使用正则表达式从Java中的path提取环境变量名,java,regex,Java,Regex,我必须使用java方法提取环境变量的值 我的路径是${rootPath}/user/settings,我想得到的值是rootPath 我尝试了以下操作,但显示“未找到匹配项”: 如果使用replace方法,它将替换${rootPath}值。我做错了什么?使用replaceAll()对我来说不是个坏主意,代码看起来很简单:): O/p: 根路径 您缺少find()调用和验证。您需要以正确的方式使用replaceAll()方法:)这不是正确的方法使用后缀和子字符串,不要过度使用regexpr s.s

我必须使用java方法提取环境变量的值

我的路径是${rootPath}/user/settings,我想得到的值是rootPath

我尝试了以下操作,但显示“未找到匹配项”:

如果使用replace方法,它将替换${rootPath}值。我做错了什么?

使用
replaceAll()
对我来说不是个坏主意,代码看起来很简单:):

O/p:

根路径


您缺少
find()
调用和验证。您需要以正确的方式使用
replaceAll()
方法:)这不是正确的方法使用后缀和子字符串,不要过度使用regexpr s.substring(0,s.length()-suffix.length())
Pattern.compile("\\$\\{(\\w+)\\}").matcher("${rootPath}/user/settings").group(1);
public static void main(String[] args) {
    String s = "${rootPath}/user/settings";
    System.out.println(s.replaceAll("\\$\\{(.*?)\\}.*","$1"));
}