我想分割这个字符串:

5+(4/2)-4+1+(4%3)!=23+(3*5)


使用regex进入列表。

输出应为:

[5 , + , ( , 4 , / , 2 , ) , - , 4 , + , 1 , + , ( , 4 , % , 3 , != , 23 , + , ( , 3 , * , 5 , ) ]


注意:我的问题完全是not equal运算符,regex不会将其视为单个字符

最佳答案

做匹配而不是分裂。

List<String> list = new ArrayList<String>();
Matcher m = Pattern.compile("[^\\w()]?=|\\w+|\\W").matcher(s);
while(m.find()) {
   list.add(m.group())
 }
System.out.println(list);


DEMO

10-02 23:54