需要更改正则表达式以获得额外的后缀标准
所以我有这个代码:
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 output = new ArrayList(); while (matcher.find()) { output.add(matcher.group()); }
当我正在解析整数时,正则表达式当然没问题,但我现在需要它考虑到可以有一个。 代表一个浮点。
想知道是否有人可以帮我添加这个
预期产量应为:
4.0 5.0 2.0 / + 7.0 - 11.0 34.0 2.0 / 3.0 / 1.0 * +
这将产生您想要的输出。 Logical OR |
旁边的部分 运算符将匹配剩余子字符串中的[-+*/$£]
符号。
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 output = new ArrayList (); 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 * +