正常情况下 string.split方法分割出来的字符串会把正则中包含的特殊字符去掉,为了达到保留字符的需求,翻了一下split源码,copy出来稍作改动,此处记录一下。
//示例字符串
String string = "Long long ago there.was a prin,cess in a castle \"which was on, the edge of the cliff. The princess was\"dreaming of marrying a handsome prince...";
//正则表达式,以空格 英文逗号、句号、引号为例
String regex = "(\\d|\\ |\\.|\\,|\\?|\\!|\\\")";
//调用方式
for (String string2 : split(string, regex)) {
L.e(string2);
}
public String[] split(CharSequence input,String regex) {
int index = 0;
ArrayList<String> matchList = new ArrayList<>();
String string = input.toString();
Pattern pattern = Pattern.compile(regex);
Matcher m = pattern.matcher(string);
//这里是targetSdkVersion>28时的处理方法,其他情况查看Pattern源码
while (m.find()) {
String match = input.subSequence(index, m.start() + 1).toString();
matchList.add(match);