高分求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行也匹配?
...全文
145 点赞 收藏 2
写回复
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));
		}
	}
}
回复 点赞
发动态
发帖子
Java SE
创建于2007-09-28

3.4w+

社区成员

30.7w+

社区内容

Java 2 Standard Edition
社区公告
暂无公告