繁体   English   中英

正则表达式拆分与分隔符同时保持分隔符

[英]Regex Split with Delimiters while keeping delimiters

我试图将带分隔符的字符串拆分为数组,同时保留分隔符。

我的字符串是:“2 + 37/4 + 26”。

我希望数组为: [2,+,37,/,4,+,26]

您可以使用lookarounds进行拆分:

String[] tok = input.split("(?<=[+*/-])|(?=[+*/-])");

RegEx演示

说明:

(?<=[+*/-])  # 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM