我有以下字符串(str):
String str="${name}/${family}/${child}/${fname}"
我在java中使用什么正则表达式模式来获取“${”和“}”之间的所有子字符串并将它们保存在数组列表中, 所以结果将是一个包含以下值的数组列表:“name”,“family”,“child”,“fname”
最佳答案
您可以使用 "\\$\\{(\\w+)\\}"
编译Pattern
,然后循环到构建一个列表
,例如,
String str = "${name}/${family}/${child}/${fname}";
Pattern pat = Pattern.compile("\\$\\{(\\w+)\\}");
Matcher mat = pat.matcher(str);
List<String> al = new ArrayList<>();
while (mat.find()) {
al.add(mat.group(1));
}
System.out.println(al);
或,就像我在评论中所说,只需一行
System.out.println(Arrays.asList(str.replaceAll("[\\$\\{\\}]", "").split("/")));
关于java - 使用正则表达式模式获取两个表达式之间的所有子字符串(JAVA),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41949595/