如下是摘录的正则表达式的规则说明:
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 |
/\<love/ |
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
|
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());
附件:
分享到:
相关推荐
JAVA正则表达式实例教程 欢迎下载
java正则表达式实例教程,为方便java程序员的开发使用
JAVA正则表达式实例教程.pdfJAVA正则表达式实例教程.pdf
JAVA正则表达式实例教程.txt
正则表达式到底是什么东西? 字符是计算机软件处理文字时最基本的单位,可能是字母,数字,标点符号,空格,换行符,汉字等等。字符串是0个或更多个字符的序列。文本也就是文字,字符串。说某个字符串匹配某个正则...
正则表达式 实例
JAVA正则表达式实例教程
对于如何使用正则表达式来解决性能不佳、误报、漏报等常见的错误以及完成一些常见的任务,《正则表达式经典实例》给出了涉及基于C#、Java、JavaScript、Perl、PHP、Python、Ruby和VB.NET等编程语言的解决方案。...
JAVA正则表达式实例教程.
/** * 匹配的例子: * (GC_F_BA_ACD_FDALFD_I_FALDJF) * (GC_F_BA_ACD_FDALFD) * (GC_F_BA_ACD)等 * * 解释 * \\( \\)表示:前后的一对小括号 * * [A-Z]+(\\_[A-Z]+)* * [A-Z]+ 1个或多个大写字母 ...
JAVA正则表达式实例教程 JAVA正则表达式实例教程
java中常用的匹配正则表达式实例大全.doc
Java正则表达式的使用实例Java正则表达式的使用实例;关于url、电话等字符串的判断
我在一次偶然中发现了位于 java.sun.com 站点上的 Java Tutorial,也在那里看到了关于 Java 的正则表达式教程,感觉它不同于其他的正则表达式教程,文中以大量的匹配实例来进行说明。为了能让 Java 学习者能更好地...