关于 Java 中 正则表达式的 MULTILINE 标志
2016-03-20 15:49
627 查看
先看看官方给出的说明:
Enables multiline mode.
In multiline mode the expressions ^ and $ match just after or just before, respectively, a line terminator or the end of the input sequence. By default these expressions only match at the beginning and the end of the entire input sequence.
Multiline mode can also be enabled via the embedded flag expression (?m).
翻译过来大概就是说:
我测试了一下,也就是说如果没有
参考:
Pattern (Java Platform SE 7 )
MULTILINE
Enables multiline mode.
In multiline mode the expressions ^ and $ match just after or just before, respectively, a line terminator or the end of the input sequence. By default these expressions only match at the beginning and the end of the entire input sequence.
Multiline mode can also be enabled via the embedded flag expression (?m).
翻译过来大概就是说:
启用多行模式。 在多行模式中,表达式 ^ 和 $ 分别匹配一个行结束符号或者输入序列的末尾。在默认情况下,这些表达式只匹配整个输入序列的开始和结尾。 多行模式也可以通过嵌入标志 ?m 来启用。
我测试了一下,也就是说如果没有
MULTILINE标志的话,
^和
$只能匹配输入序列的开始和结束;否则,就可以匹配输入序列内部的行结束符。测试代码如下:
package cn.chd.test.regex; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.junit.Test; public class MultiLineTest { private String input = "abc\r\ndef"; /* result: test1() begin 0-3 5-8 test1() end */ @Test public void test1() { String regex = "^\\w+$"; Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE); Matcher matcher = pattern.matcher(input); System.out.println("test1() begin"); while (matcher.find()) { System.out.printf("%d-%d\n", matcher.start(), matcher.end()); } System.out.println("test1() end"); } /* result: test2() begin test2() end */ @Test public void test2() { String regex = "^\\w+$"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(input); System.out.println("test2() begin"); while (matcher.find()) { System.out.printf("%d-%d\n", matcher.start(), matcher.end()); } System.out.println("test2() end"); } }
参考:
Pattern (Java Platform SE 7 )
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- msql 正则表达式
- 正则表达式
- 插入排序
- 冒泡排序
- 堆排序