![](/img/trans.png)
[英]How to split text between different delimiters while keeping delimiters in Java?
[英]Regex Split with Delimiters while keeping delimiters
我试图将带分隔符的字符串拆分为数组,同时保留分隔符。
我的字符串是:“2 + 37/4 + 26”。
我希望数组为: [2,+,37,/,4,+,26]
您可以使用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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.