![](/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.