62,621
社区成员
发帖
与我相关
我的任务
分享
import java.util.regex.*;
public class Test02 {
public static void main(String[] args){
String str = "abc\nijk\nxyz\n";
Pattern p = Pattern.compile(".*\\n$?");
Matcher m = p.matcher(str);
while (m.find()) {
System.out.println(m.group());
}
}
}
正则最后加个?也可以,先让字符吃掉匹配的
import java.util.regex.*;
public class Test02 {
public static void main(String[] args){
String str = "abc\nijk\nxyz\n";
Pattern p = Pattern.compile(".*\\n");
Matcher m = p.matcher(str);
while (m.find()) {
System.out.println(m.group());
}
}
}
输出:
abc
ijk
xyz
由于你的代码里的正则有$,$是整个字符串结尾的意思。