正则表达式与多行模式(Java)中的空字符串不匹配 [英] Regular expression doesn't match empty string in multiline mode (Java)

查看:134
本文介绍了正则表达式与多行模式(Java)中的空字符串不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚观察到这种行为;

I just observed this behavior;

Pattern p1 = Pattern.compile("^$");
Matcher m1 = p1.matcher("");
System.out.println(m1.matches()); /* true */

Pattern p2 = Pattern.compile("^$", Pattern.MULTILINE);
Matcher m2 = p2.matcher("");
System.out.println(m2.matches()); /* false */

令我觉得最后一句话是假的奇怪。这就是文档所说的;

It strikes me as odd that the last statement is false. This is what the docs say;


默认情况下,正则表达式^和$忽略行终止符,并且只分别匹配整个输入的开头和结尾序列。如果激活MULTILINE模式,则^在输入开始时和任何行终止符之后匹配,但输入结束时除外。当处于MULTILINE模式时,$匹配行终止符或输入序列的结尾。 http://docs.oracle.com/javase /1.4.2 ......

从我得到的,它应该匹配?以下使事情变得更加混乱;

From what I get from this, it should match? The following makes things even more confusing;

Pattern p3 = Pattern.compile("^test$");
Matcher m3 = p3.matcher("test");
System.out.println(m3.matches()); /* true */

Pattern p4 = Pattern.compile("^test$", Pattern.MULTILINE);
Matcher m4 = p4.matcher("test");
System.out.println(m4.matches()); /* true */

那这是什么?我怎么理解这个?我希望有人可以对此有所了解,真的很感激。

So what is this? How do I make sense of this? I hope someone can shed some light on this, would be really appreciated.

推荐答案


如果激活MULTILINE模式,那么^匹配
输入的开头和在任何行终止符之后,除了在输入结束时。

If MULTILINE mode is activated then ^ matches at the beginning of input and after any line terminator except at the end of input.

由于你在输入的末尾, ^ 在多线模式下无法匹配。

Since you are at the end of input, ^ can't match in multiline mode.

这是令人惊讶的,甚至令人作呕,但仍然根据其文档。

This is surprising, even disgusting, but nevertheless according to its documentation.

这篇关于正则表达式与多行模式(Java)中的空字符串不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆