如下是摘录的正则表达式的规则说明:
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]^为限制开头
实例场景 电话号码有效性检测正则使用:
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());
附件: