字符串 S=“乘 3 加加 3 3 1”
我想得到两个字符串数组
第一个是{“乘”,“加”,“加”}
另一个输出是 {"3","3","3",1}
我怎么才能得到它?我尝试使用
String operators[] = s.split("[0-9]+");
String operands[] =s.split("(?:add|multiply)");
但是,这不起作用。
你应该使用Matcher
代替split
:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
...
List<String> operators = new ArrayList<String>();
Matcher m = Pattern.compile("add|multiply").matcher(s);
while (m.find()) {
operators.add(m.group());
}
List<String> operands = new ArrayList<String>();
Matcher m = Pattern.compile("[0-9]+").matcher(s);
while (m.find()) {
operands.add(m.group());
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)