高分求java regular expression.

possibleonline 2017-08-28 04:02:42
有以下需求,文本如下
new line
__new001_tag00_tag
#SW
this is news 001

abbbbbbbbbbbb
new line
__new001_tag00_tag
#SW
this is news 002
abbbbbbbbbbbb
new line
new line
__new001_tag00_tag
#SW
this is news 003
abbbbbbbbbbbb

我写成这样子实现抓取3条记录:\\r\\n(\\w*?_\\w*?_\\w*?_\\w*?)\\r\\n(.*?#SW.*?)\\r\\n(.*?)\\r\\n\\ Pattern.DOTALL
现在只能匹配粗体3行,不能匹配4行,怎么样才能把第4行也匹配?
...全文
198 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
rickylin86 2017-08-28
  • 打赏
  • 举报
回复
result: ---------- java ---------- __new001_tag00_tag #SW this is news 001 abbbbbbbbbbbb Output completed (0 sec consumed) - Normal Termination
rickylin86 2017-08-28
  • 打赏
  • 举报
回复

import java.util.regex.Pattern;
import java.util.regex.Matcher;


public class Test{
	public static void main(String[] args){
		String content = "new line\n"+
						"__new001_tag00_tag\n"+
						"#SW\n"+
						"this is news 001\n"+
						"abbbbbbbbbbbb\n"+
						"new line\n"+
						"__new001_tag00_tag\n"+
						"#SW\n"+
						"this is news 002\n"+
						"abbbbbbbbbbbb\n"+
						"new line\n"+
						"new line\n"+
						"__new001_tag00_tag\n"+
						"#SW\n"+
						"this is news 003\n"+
						"abbbbbbbbbbbb\n";
		String regex = "(?s)(?:new line\n)(.*?)(?=new line\n)";
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(content);
		if(matcher.find()){
			System.out.println(matcher.group(1));
		}
	}
}

62,634

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧