java 正则表达式 实例说明

如下是摘录的正则表达式的规则说明:

Metacharacter Function Example What It Matches
^ Beginning-of-line anchor /^love/ Matches all lines beginning with love.
$ End-of-line anchor /love$/ Matches all lines ending with love.
. Matches one character /l..e/ Matches lines containing an l, followed by two characters, followed by an e.
* Matches zero or more of the preceding characters / *love/ Match lines with zero or more spaces, followed by the patternlove.
[ ] Matches one in the set /[Ll]ove/ Matches lines containing love or Love.
[x–y] Matches one character within a range in the set /[A–Z]ove/ Matches letters from A through Z followed by ove.
[^ ] Matches one character not in the set /[^A–Z]/ Matches any character not in the range betweenA andZ.
\ Used to escape a metacharacter /love\./ Matches lines containing love, followed by a literal period. Normally the period matches one of any character.
Additional metacharacters are supported by many UNIX programs that use RE metacharacters:
\< Beginning-of-word anchor /\ Matches lines containing a word that begins withlove (supported byvi andgrep).
\> End-of-word anchor /love\>/ Matches lines containing a word that ends withlove (supported byvi andgrep).
\(..\) Tags match characters to be used later /\(love\)able \1er/ May use up to nine tags, starting with the first tag at the left-most part of the pattern. For example, the patternlove is saved as tag 1, to be referenced later as\1; in this example, the search pattern consists oflovable followed bylover (supported bysed, vi, andgrep).
x{m\}or x{m,\}or x{m,n\} Repetition of character x, m times, at least m times, at least m and not more than n times[a] o{5,10\} Matches if line contains between 5 and 10 consecutive occurrences of the letter o (supported byvi andgrep).

实例说明:

预定义字符类 
. 任何字符(与行结束符可能匹配也可能不匹配) 
\d 数字:[0-9] 
\D 非数字: [^0-9] 
\s 空白字符:[ \t\n\x0B\f\r] 
\S 非空白字符:[^\s] 
\w 单词字符:[a-zA-Z_0-9] 
\W 非单词字符:[^\w] 
^为限制开头
^java条件限制为以Java为开头字符


$为限制结尾
java$条件限制为以java为结尾字符


.条件限制除\n以外任意一个单独字符
java..条件限制为java后除换行外任意两个字符


加入特定限制条件[]
[a-z]条件限制在小写atoz范围中一个字符
[A-Z]条件限制在大写AtoZ范围中一个字符
[a-zA-Z]条件限制在小写atoz或大写AtoZ范围中一个字符
[0-9]条件限制在小写0to9范围中一个字符
[0-9a-z]条件限制在小写0to9或atoz范围中一个字符


[0-9[a-z]]条件限制在小写0to9或atoz范围中一个字符(并集)
[a-z&&[aeiou]]条件限制在atoz范围与aeiou字符的(交集)
[]中加入^后加再次限制条件[^]
[^a-z]条件限制在非小写atoz范围中一个字符
[^A-Z]条件限制在非大写AtoZ范围中一个字符
[^a-zA-Z]条件限制在非小写atoz或大写AtoZ范围中一个字符
[^0-9]条件限制在非小写0to9范围中一个字符
[^0-9a-z]条件限制在非小写0to9或atoz范围中一个字符
[^0-9[a-z]]条件限制在非小写0to9或atoz范围中一个字符


集锦复习:
[abc] a、b 或 c(简单类)
[^abc] 任何字符,除了 a、b 或 c(否定)
[a-zA-Z] a 到 z 或 A 到 Z,两头的字母包括在内(范围)
[a-d[m-p]] a 到 d 或 m 到 p:[a-dm-p](并集)
[a-z&&[def]] d、e 或 f(交集)
[a-z&&[^bc]] a 到 z,除了 b 和 c:[ad-z](减去)
[a-z&&[^m-p]] a 到 z,而非 m 到 p:[a-lq-z](减去)


在限制条件为特定字符出现0次以上时,可以使用「*」J*0个以上J
.* 0个以上任意字符
J.*D J与D之间0个以上任意字符


在限制条件为特定字符出现1次以上时,可以使用「+」J+1个以上J
.+ 1个以上任意字符
J.+D J与D之间1个以上任意字符


实例场景 电话号码有效性检测正则使用:

public static boolean phoneFormat(String phone){
final String pattern1 = ("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");

//等同于 "^((13[0-9])|(15[\\d&&[^4]])|(18[0,5-9]))\\d{8}$" 其中[\\d&&[^4]]表示交集 0-9同时非4 交集就是【0-35-9】

//正则检测合法的手机号码

//130 131 132 133 134 135 136 137 138 139

//150 151 152 153 155 156 157 158 159

//180 185 186 187 188 189
final Pattern pattern = Pattern.compile(pattern1);
final Matcher mat = pattern.matcher(phone);
if (!mat.matches()) {
return false;
}
return true;
}


实例场景 邮箱号码有效性检测正则使用:

final String pattern2 = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";

//([-+.]\\w+)* ()内表示 :-+.以及a-zA-Z_0-9 至少一个 ,()*表示:0-任意个()中内容
final Pattern pattern = Pattern.compile(pattern2);
final Matcher mat = pattern.matcher(email);
System.out.println(mat.matches());



附件:

8Java 正则表达式工具开源软件


你可能感兴趣的:(java)