我有一个很长的句子,其中嵌入了新的行或回车符,我想将它们分成单独的句子。这样一个例子: This is a new line=?xxx\n What's \n up
应该产生This is a new line=?xxx
What's
和up
使用Java以新行作为分隔符来分割句子(1.7)匹配器
我不想使用String.split("\n")
而是类似:
String x = " This is a new line=?xxx\n Whats' \n up";
// This is not correct
Pattern p = Pattern.compile("(.*[\r\n]+|$)");
Matcher m = p.matcher(x);
while (m.find()) {
System.out.printline(m.group(1));
}
以上代码生成:
This is a new line=?xxx
What's
什么问题与我的正则表达式?
在最后\ n之后找不到任何东西。所以m.find()返回false。 –
'我不想使用String.split(“\ n”)'你能澄清原因吗? – anubhava
为什么不使用'。+'来匹配*行*? –