java 正则”.”匹配换行符

Java 如何匹配换行符呢?  查阅资料发现, “.” 并不是一定可以匹配所有字符,例如\r\n ,  “.”  并不一定就能匹配到.  资料也进行了说明. 但遗憾的是并没有说哪些情况下不能匹配到, 哪些情况可以匹配到.    在windows vista , jdk1.6 中, 经过我测试无法用”.” 正则表达式中所说的”.”匹配任意字符来匹配到换行符.
所以呢,我们得曲线救国了.  例如可以用 [^\\] 的方式,  例如该意思是所有不包含\的字符,那么自然可以包含换行符了. 缺点,你需要指定一个不需要匹配的字符. 
. any character at all
(not new line in some circumstances)
Operator Type Examples Description
Literal Characters
Match a character exactly
a A y 6 % @ Letters, digits and many special
characters match exactly
\$ \^ \+ \\ \? Precede other special characters
with a \ to cancel their regex special meaning
\n \t \r Literal new line, tab, return
\cJ \cG Control codes
\xa3 Hex codes for any character
Anchors and assertions ^ Starts with
$ Ends with
\b \B on a word boundary,
NOT on a word boundary
Character groups
any 1 character from the group
[aAeEiou] any character listed from [ to ]
[^aAeEiou] any character except aAeEio or u
[a-fA-F0-9] any hex character (0 to 9 or a to f)
. any character at all
(not new line in some circumstances)
\s any space character (space \n \r or \t)
\w any word character (letter digit or _)
\d any digit (0 through 9)
\S \W \D any character that is NOT a space
word character or digit
apply to previous element
+ 1 or more (“some”)
* 0 or more (“perhaps some”)
? 0 or 1 (“perhaps a”)
{4} exactly 4
{4,} 4 or more
{4,8} between 4 and 8
Add a ? after any count to turn it sparse (match as few as possible) rather than have it default to greedy
Alternation | either, or
Grouping ( ) group for count and save to variable
(?: ) group for count but do not save
Variables $xyz Insert contents of $xyz into regular expression
\1 \2 Back reference to 1st, 2nd etc matched groups

此篇文章已被阅读2284 次

Add a Comment

邮箱地址不会被公开。 必填项已用*标注