需要更改正则表达式以获得其他后缀标准 [英] need to change regex for additional postfix criteria
本文介绍了需要更改正则表达式以获得其他后缀标准的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我有这个代码:
Pattern pattern = Pattern.compile("\\d*(\\s\\d+\\.)*\\s*[-\\+\\*/\\$£]");
String input = "4.0 5.0 2.0 / + 7.0 - 11.0 34.0 2.0 / 3.0 / 1.0 * +";
Matcher matcher = pattern.matcher(input);
List<String> output = new ArrayList<>();
while (matcher.find()) {
output.add(matcher.group());
}
当我正在解析整数时,正则表达式当然没问题但是我现在需要它考虑到可以有一个。代表一个浮点。
When i was working on just parsing integers the regex was of course fine however i now need it to take into account that there can be a . to represent a floating point.
想知道是否有人可以帮我添加这个
Wondering if someone can help me with adding this in
预期输出应该是:
4.0 5.0 2.0 /
+
7.0 -
11.0 34.0 2.0 /
3.0 /
1.0 *
+
推荐答案
这将产生您想要的输出。逻辑OR |
运算符旁边的部分将匹配剩余的 [ - + * / $£]
符号子字符串。
This would produce the output you want. The part next to the Logical OR |
operator will matches the [-+*/$£]
symbols from the remaining sub-string.
Pattern pattern = Pattern.compile("\\d+(?:\\.\\d+)?(\\s\\d+(?:\\.\\d+)?)*\\s*[-+*/$£]|[-+*/$£]");
String input = "4.0 5.0 2.0 / + 7.0 - 11.0 34.0 2.0 / 3.0 / 1.0 * +";
Matcher matcher = pattern.matcher(input);
ArrayList<String> output = new ArrayList<String>();
while (matcher.find()) {
output.add(matcher.group());
}
for (String s: output)
{
System.out.println(s);
}
输出:
4.0 5.0 2.0 /
+
7.0 -
11.0 34.0 2.0 /
3.0 /
1.0 *
+
这篇关于需要更改正则表达式以获得其他后缀标准的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文