Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.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中找到特定模式的字符串值?_Java_Regex - Fatal编程技术网

如何在java中找到特定模式的字符串值?

如何在java中找到特定模式的字符串值?,java,regex,Java,Regex,在我的页面中,我有几个具有相同模式的值,如${rate+0.0020D2}、${rate-0.4002D3}、${rate+0.2003D4} 基本上,模式是${[rate][+或-sign][a neumeric value][D2或D3或D4]} 所以我想在jsp中找到所有具有这些模式的值,并将它们存储在数组中 要找出此模式的价值,必须应用哪个正则表达式模式。可能有更好的方法,但这是我对它的基本看法 String regExp = "\\$\\{rate[+-]\\d+(\\.\\d+)?D

在我的页面中,我有几个具有相同模式的值,如
${rate+0.0020D2}、${rate-0.4002D3}、${rate+0.2003D4}

基本上,模式是${[rate][+或-sign][a neumeric value][D2或D3或D4]}

所以我想在jsp中找到所有具有这些模式的值,并将它们存储在数组中


要找出此模式的价值,必须应用哪个正则表达式模式。

可能有更好的方法,但这是我对它的基本看法

String regExp = "\\$\\{rate[+-]\\d+(\\.\\d+)?D[0-9]\\}";
String value = "${rate+0.0020D2},banana,${rate-0.4002D3},${rate+0.2003D4},${rate+bananD4},${rate+.123.415D4}";

Pattern pattern = Pattern.compile(regExp);
Matcher matcher = pattern.matcher(value);

String match = null;

List<String> matches = new ArrayList<String>(5);
while (matcher.find()) {

    int startIndex = matcher.start();
    int endIndex = matcher.end();

    match = matcher.group();
    matches.add(match);

}
String[] results = matches.toArray(new String[0]);

System.out.println(Arrays.toString(results));

也许有更好的方法可以做到这一点,但这是我对它的基本看法

String regExp = "\\$\\{rate[+-]\\d+(\\.\\d+)?D[0-9]\\}";
String value = "${rate+0.0020D2},banana,${rate-0.4002D3},${rate+0.2003D4},${rate+bananD4},${rate+.123.415D4}";

Pattern pattern = Pattern.compile(regExp);
Matcher matcher = pattern.matcher(value);

String match = null;

List<String> matches = new ArrayList<String>(5);
while (matcher.find()) {

    int startIndex = matcher.start();
    int endIndex = matcher.end();

    match = matcher.group();
    matches.add(match);

}
String[] results = matches.toArray(new String[0]);

System.out.println(Arrays.toString(results));
试试这个。看演示。抓取
组1

试试这个。看演示。抓取
组1


您只想要
0.0020D2
部分正确吗?不,我想要${rate+0.0020D2}的整个值,我想要得到rate+0.0020D2。问题是“+”0.0020“和“D2”b可以更改,所以我如何才能获得与此模式类似的所有值您只需要
0.0020D2
部分正确?不,我想要整个值,如${rate+0.0020D2}我想要获得rate+0.0020D2。问题是“+”0.0020“和“D2”b可以更改,所以我如何才能获得与此模式类似的所有值谢谢!!你能分享这本书或链接来学习正则表达式模式吗?我倾向于参考我能找到的大多数正则表达式测试人员,比如course@vks我猜应该是
[0-9\.]
:P,对
${rate+sometingD4}形式的值进行折扣
..@vks这里没有参数,只使用
[0-9]
这可能对meYes不利,否则它似乎会抛出“线程中的异常”main“java.util.regex.PatternSyntaxException:索引1附近的非法重复”谢谢朋友!!你能分享这本书或链接来学习正则表达式模式吗?我倾向于参考我能找到的大多数正则表达式测试人员,比如course@vks我猜应该是
[0-9\.]
:P,对
${rate+sometingD4}形式的值进行折扣
..@vks此处没有参数,只需使用
[0-9]
,这可能对meYes不利,否则它似乎会抛出“线程中的异常”main“java.util.regex.PatternSyntaxException:索引1附近的非法重复”
\${(rate[+-]\d+(?:\.\d+)?(?:D2|D3|D4))}