Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
114 views
in Technique[技术] by (71.8m points)

java - Is " " a vertical whitespace, i.e., should "v" match it?

Logically, it is (but logic is irrelevant whenever character encodings or locales are in play). According to

perl -e 'print "
" =~ /v/ ? "y
" : "n
";'

printing "y", it is. According to

Pattern.compile("\v").matcher("
").matches();

returning false in java, it's not. This wouldn't confuse me at all, if there weren't this posting claiming that

Sun’s updated Pattern class for JDK7 has a marvelous new flag, UNICODE_CHARACTER_CLASS, which makes everything work right again.

But I'm using java version "1.7.0_07" and the flag exists and seems to change nothing at all. Moreover, " " 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Java 7's 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 doesn't belong to Java's category of "vertical whitespace"; it's that Java 7 doesn't have a category of "vertical whitespace". Instead, Java 7 regexes have an undocumented feature whereby they interpret v as referring to the vertical tab character, U+000B. (This is a traditional escape sequence from C/C++/Bash/etc., though Java string literals don't support it. Likewise with a for alert/bell and cX for control-character X.)

Edited to add: This has changed in newer versions of Java. According to Java 8's Javadoc for java.util.regex.Pattern, v now means "A vertical whitespace character: [ x0Bf x85u2028u2029]".


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...