使用更强大的
Matcher功能代替
String.split。下面的代码应该可以使用,但是尚未进行优化:
Pattern pattern = Pattern.compile("\d*(\$|£)");String input = "1£23$456$£$";Matcher matcher = pattern.matcher(input);List<String> output = new ArrayList<>();while (matcher.find()) { output.add(matcher.group());}打印
output.toString()产生:
[1£, 23$, 456$, £, $]
更新的要求:
- 还包括分隔符:
+
,-
,*
,和/
- 非定界符字符只是在定界符前带有可选空格的数字。
- 任何此类空格都是值的一部分,而不是分隔符本身。
使用正则表达式:
\d*\s*[-\+\*/\$£]
该模式,具有给定的输入:
1£23$456$£$7+89-1011*121314/1 £23 $456 $ £ $7 +89 -1011 * 121314 /
将生成此输出:
[1£, 23$, 456$, £, $, 7+, 89-, 1011*, 121314/, 1 £, 23 $, 456 $, £, $, 7 +,89 -, 1011 *, 121314 /]



