java "\n" 是一个垂直的空格,即"\v" 应该匹配吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12290224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is "\n" a vertical whitespace, i.e., should "\v" match it?
提问by maaartinus
Logically, it is (but logic is irrelevant whenever character encodings or locales are in play). According to
从逻辑上讲,它是(但只要字符编码或语言环境在起作用,逻辑就无关紧要)。根据
perl -e 'print "\n" =~ /\v/ ? "y\n" : "n\n";'
printing "y", it is. According to
打印“y”,它是。根据
Pattern.compile("\v").matcher("\n").matches();
returning false
in java, it's not. This wouldn't confuse me at all, if there weren't this postingclaiming that
false
在java中返回,它不是。如果没有这个帖子声称,这根本不会让我感到困惑
Sun's updated Pattern class for JDK7 has a marvelous new flag, UNICODE_CHARACTER_CLASS, which makes everything work right again.
Sun 为 JDK7 更新的 Pattern 类有一个了不起的新标志 UNICODE_CHARACTER_CLASS,它使一切恢复正常。
But I'm using java version "1.7.0_07" and the flag exists and seems to change nothing at all. Moreover, "\n" is no newcomer to Unicode but a plain old ASCII character, so I really don't see how this difference may happen. Probably I'm doing something stupid, but I can't see it.
但是我使用的是 Java 版本“1.7.0_07”并且该标志存在并且似乎根本没有改变。此外,"\n" 不是 Unicode 的新手,而是一个普通的旧 ASCII 字符,所以我真的不明白这种差异是如何发生的。可能我在做一些愚蠢的事情,但我看不到它。
回答by ruakh
The Javadoc for java.util.regex.Pattern
explicitly mentions \v
in its "list of Perl constructs not supported by this class". So it's not that \n
doesn't belong to Java's category of "vertical whitespace"; it's that Java doesn't havea category of "vertical whitespace".
Javadoc forjava.util.regex.Pattern
\v
在其“此类不支持的 Perl 构造列表”中明确提及。所以它并不是\n
不属于 Java 的“垂直空白”类别;它是Java不具备“垂直空白”的范畴。
Edited to add:Instead, \v
stands for the vertical tab character, U+000B. This is a traditional escape sequence; there are also a few other traditional escape sequences that aren't allowed in Java string literals but that are supported by Pattern
(\a
for alert/bell, \cX
for control-character X
). Oddly, however, the Javadoc for Pattern
fails to mention that it supports \v
; so I'm not sure if it can be expected to be supported in all JDK implementations.
编辑添加:相反,\v
代表垂直制表符,U+000B。这是一个传统的转义序列;还有一些其他传统的转义序列在 Java 字符串文字中是不允许的,但由Pattern
(\a
对于警报/铃铛,\cX
对于控制字符X
) 支持。然而奇怪的是,Javadoc forPattern
没有提到它支持\v
; 所以我不确定它是否可以在所有 JDK 实现中得到支持。
回答by Keith Thompson
perldoc perlrecharclass
says that \v
matches a "vertical whitespace character". This is further explained:
perldoc perlrecharclass
表示\v
匹配“垂直空白字符”。进一步解释如下:
"\v" matches any character considered vertical whitespace; this includes the platform's carriage return and line feed characters (newline) plus several other characters, all listed in the table below. "\V" matches any character not considered vertical whitespace. They use the platform's native character set, and do not consider any locale that may otherwise be in use.
"\v" 匹配任何被认为是垂直空白的字符;这包括平台的回车符和换行符(换行符)以及其他几个字符,所有这些都列在下表中。"\V" 匹配任何不被视为垂直空白的字符。它们使用平台的本机字符集,并且不考虑可能正在使用的任何语言环境。
Specifically, \v
matches the following characters in 5.16:
具体\v
匹配5.16中的以下字符:
$ unichars -au '\v' # From Unicode::Tussle
---- U+0000A LINE FEED
---- U+0000B LINE TABULATION
---- U+0000C FORM FEED
---- U+0000D CARRIAGE RETURN
---- U+00085 NEXT LINE
---- U+02028 LINE SEPARATOR
---- U+02029 PARAGRAPH SEPARATOR
You could use a character class to get the same effect as Perl's \v
.
您可以使用字符类来获得与 Perl 的\v
.
Of course this applies to Perl; I don't know whether it applies to Java.
当然,这适用于 Perl;我不知道它是否适用于Java。