【发布时间】:2017-11-09 17:27:28
【问题描述】:
所以我正在尝试使用正则表达式和 java 中的 split 函数来拆分字符串。 当像这样的非大写字母后有大写字母时,正则表达式应该拆分字符串
hHere // -> should split to ["h", "Here"]
我正在尝试像这样拆分字符串
String str = "1. Test split hHere and not .Here and /Here";
String[] splitString = str.split("(?=\\w+)((?=[^\\s])(?=\\p{Upper}))");
/* print splitString */
// -> should split to ["1. Test split h", "Here and not .Here and not /Here"]
for(String s : splitString) {
System.out.println(s);
}
我得到的输出
1.
Test split h
Here and not .
Here and /
Here
我想要的输出
1. Test split h
Here and not .Here and not /Here
只是无法弄清楚执行此操作的正则表达式
【问题讨论】:
-
诀窍是使用“lookbehind”和“lookahead”。使用
(?<=指定拆分前的内容,(?=指定拆分后的内容。 -
Thx 都成功了,但 (?