我正在尝试将带分隔符的字符串拆分为数组,同时保留分隔符。
我拥有的字符串是:“2 + 37/4 + 26”。
我希望数组为:[2,+,37,/,4,+,26]
答案 0 :(得分:3)
您可以使用lookarounds进行拆分:
String[] tok = input.split("(?<=[+*/-])|(?=[+*/-])");
<强>解释强>
(?<=[+*/-]) # when preceding character is one of 4 arithmetic operators
| # regex alternation
(?=[+*/-]) # when following character is one of 4 arithmetic operators