我想分割这个字符串:
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