Split string with multiple delimiter including delimiters

荒凉一梦 提交于 2021-02-07 18:16:29

问题


I would like to split a string using multiple character delimiters, but I also want to store delimiters. My delimiters are ()+-*/

So for example, if I had a string

26+78(12*23)-16

I want to get

26

+

78

(

12

*

23

)

-

16

each line as a separate array element.

I think you can not use split function to achieve this. However, my trial with string-tokenizer also failed. How can I achieve this?


回答1:


I wouldn't answer if it wasn't saturday night here:

    String s1 = "26+78(12*23)-16";
    for(String s: s1.split("(?<=[()+*/-])|(?=[()+*/-])")){
        System.out.println(">> " + s);
    }

gives:

>> 26
>> +
>> 78
>> (
>> 12
>> *
>> 23
>> )
>> -
>> 16


来源:https://stackoverflow.com/questions/13894724/split-string-with-multiple-delimiter-including-delimiters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!