正则Java

System.out.print("abc".matches("...");

  打印结果为true

1 "a8729a".replaceAll("\\d","-")

a8729a替换成a----a

数字的正则是\d,但在java里要 \\d 才能识别

Patern p=Pattern.compile("[a-z]{3}");
Matcher m=p.matcher("fgh")
m.matcher();

  结果为true

"fgha".matches("[a-z]{3}");

  结果为false。运行效率没有上面三句高。

(1)  .    *   +   ?

.   :任意一个字符

*   :零个或多个

+  :一个或多个

?   :一个或零个

(2)[ ]     { }

[ ]     表示范围,只匹配一个字符

    [^abc]    除了abc以外的字符,^取反

    [a-zA-z]    a到z或A到Z,与[a-z]|[A-Z],[a-z[A-Z]]都一样

    [A-Z&&[RFG]]    A到Z并且在RFG中的

{ }     表示次数

(3)   \s   \w   \d   \

\d   [0-9]

\s   [\t\n\x0B\f\r]

\w  [a-zA-Z_0-9]    a-z,A-Z,_,0-9

当是大写字母时是以上对应取反

\     java表示一个\用"\\"表示,而正则就要"\\\\"表示

猜你喜欢

转载自www.cnblogs.com/binbinbinhahaha/p/9342682.html