java正则表达式匹配多行文本
By:Roy.LiuLast updated:2013-11-28
在默认情况下,正则表达式中的"." 能匹配很多字符,但却不包括换行符。为了使正则表达式匹配多行,需要增加(?s)或者采用Pattern.DOTALL .下面用一个简单的例子说明.
有如下文本,需要过滤Address 1的内容。
先采用如下方式过滤
结果错误
继续试验:
结果正确。整个例子如下:
运行结果如下;
其他正则表达式参考文档:正则表达式参考
有如下文本,需要过滤Address 1的内容。
Starting... Address 1: 88 app 2/8 superman taman, puchong 36100, Malaysia Address 2: abc End
先采用如下方式过滤
Address 1:\\s(.*)Address 2:
结果错误
继续试验:
(?s)Address 1:\\s(.*)Address 2: 或者采用 Pattern.compile(Address 1:\\s(.*)Address 2:, Pattern.DOTALL);
结果正确。整个例子如下:
import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; public class ReTest{ private Pattern addressPattern = Pattern.compile(ADDRESS_PATTERN); private Matcher matcher; //可选 /*private Pattern addressPattern = Pattern.compile(ADDRESS_PATTERN, Pattern.DOTALL);*/ private static final String ADDRESS_PATTERN = "(?s)Address 1:\\s(.*)Address 2:"; public static void main(String[] args) { String data = "Testing... \n" + "Address 1: 88 app 2/8\n" + "superman taman, puchong\n" + "36100, Malaysia\n" + "Address 2: abc" + "testing end"; ReTest obj = new ReTest(); Listlist = obj.getAddress(data); System.out.println("原始数据 : "); System.out.println(data + "\n"); System.out.println("============================"); System.out.println("re查找结果 : " + list.get(0)); } private List getAddress(String data){ List result = new ArrayList (); matcher = addressPattern.matcher(data); while (matcher.find()) { result.add(matcher.group(1)); } return result; } }
运行结果如下;
原始数据 : Testing... Address 1: 88 app 2/8 superman taman, puchong 36100, Malaysia Address 2: abctesting end ============================ re查找结果 : 88 app 2/8 superman taman, puchong 36100, Malaysia
其他正则表达式参考文档:正则表达式参考
From:一号门
COMMENTS