简介
正则表达式(regular expression)描述了一种字符串匹配的模式(pattern),可以用来检查一个串是否含有某种子串、将匹配的子串替换或者从某个串中取出符合某个条件的子串等。
字符
描述
\
将下一个字符标记为一个特殊字符、或一个原义字符、或一个向后引用、或一个八进制转义符。例如,“n”匹配字符“n”。“\n”匹配一个换行符。串行“\\”匹配“\”而“\(”则匹配“(”。
^
匹配输入字符串的开始位置。如果设置了RegExp对象的Multiline属性,^也匹配“\n”或“\r”之后的位置。
$
匹配输入字符串的结束位置。如果设置了RegExp对象的Multiline属性,$也匹配“\n”或“\r”之前的位置。
*
匹配前面的子表达式零次或多次。例如,zo*能匹配“z”以及“zoo”。*等价于{0,}。
+
匹配前面的子表达式一次或多次。例如,“zo+”能匹配“zo”以及“zoo”,但不能匹配“z”。+等价于{1,}。
?
匹配前面的子表达式零次或一次。例如,“do(es)?”可以匹配“does”或“does”中的“do”。?等价于{0,1}。
{n}
n是一个非负整数。匹配确定的n次。例如,“o{2}”不能匹配“Bob”中的“o”,但是能匹配“food”中的两个o。
{n,}
n是一个非负整数。至少匹配n次。例如,“o{2,}”不能匹配“Bob”中的“o”,但能匹配“foooood”中的所有o。“o{1,}”等价于“o+”。“o{0,}”则等价于“o*”。
{n,m}
m和n均为非负整数,其中n<=m。最少匹配n次且最多匹配m次。例如,“o{1,3}”将匹配“fooooood”中的前三个o。“o{0,1}”等价于“o?”。请注意在逗号和两个数之间不能有空格。
?
当该字符紧跟在任何一个其他限制符(*,+,?,{n},{n,},{n,m})后面时,匹配模式是非贪婪的。非贪婪模式尽可能少的匹配所搜索的字符串,而默认的贪婪模式则尽可能多的匹配所搜索的字符串。例如,对于字符串“oooo”,“o+?”将匹配单个“o”,而“o+”将匹配所有“o”。
.
匹配除“\n”之外的任何单个字符。要匹配包括“\n”在内的任何字符,请使用像“(.
\n)”的模式。
(pattern)
匹配pattern并获取这一匹配。所获取的匹配可以从产生的Matches集合得到,在VBScript中使用SubMatches集合,在JScript中则使用$0…$9属性。要匹配圆括号字符,请使用“\(”或“\)”。
(?:pattern)
匹配pattern但不获取匹配结果,也就是说这是一个非获取匹配,不进行存储供以后使用。这在使用或字符“(
)”来组合一个模式的各个部分是很有用。例如“industr(?:y
ies)”就是一个比“industry
industries”更简略的表达式。
(?=pattern)
正向肯定预查,在任何匹配pattern的字符串开始处匹配查找字符串。这是一个非获取匹配,也就是说,该匹配不需要获取供以后使用。例如,“Windows(?=95
98
NT
2000)”能匹配“Windows2000”中的“Windows”,但不能匹配“Windows3.1”中的“Windows”。预查不消耗字符,也就是说,在一个匹配发生后,在最后一次匹配之后立即开始下一次匹配的搜索,而不是从包含预查的字符之后开始。
(?!pattern)
正向否定预查,在任何不匹配pattern的字符串开始处匹配查找字符串。这是一个非获取匹配,也就是说,该匹配不需要获取供以后使用。例如“Windows(?!95
98
NT
2000)”能匹配“Windows3.1”中的“Windows”,但不能匹配“Windows2000”中的“Windows”。预查不消耗字符,也就是说,在一个匹配发生后,在最后一次匹配之后立即开始下一次匹配的搜索,而不是从包含预查的字符之后开始
(?<=pattern)
反向肯定预查,与正向肯定预查类拟,只是方向相反。例如,“(?<=95
98
NT
2000)Windows”能匹配“2000Windows”中的“Windows”,但不能匹配“3.1Windows”中的“Windows”。
(?<!pattern)
反向否定预查,与正向否定预查类拟,只是方向相反。例如“(?<!95
98
NT
2000)Windows”能匹配“3.1Windows”中的“Windows”,但不能匹配“2000Windows”中的“Windows”。
x
y
匹配x或y。例如,“z
food”能匹配“z”或“food”。“(z
f)ood”则匹配“zood”或“food”。
[xyz]
字符集合。匹配所包含的任意一个字符。例如,“[abc]”可以匹配“plain”中的“a”。
[^xyz]
负值字符集合。匹配未包含的任意字符。例如,“[^abc]”可以匹配“plain”中的“p”。
[a-z]
字符范围。匹配指定范围内的任意字符。例如,“[a-z]”可以匹配“a”到“z”范围内的任意小写字母字符。
[^a-z]
负值字符范围。匹配任何不在指定范围内的任意字符。例如,“[^a-z]”可以匹配任何不在“a”到“z”范围内的任意字符。
\b
匹配一个单词边界,也就是指单词和空格间的位置。例如,“er\b”可以匹配“never”中的“er”,但不能匹配“verb”中的“er”。
\B
匹配非单词边界。“er\B”能匹配“verb”中的“er”,但不能匹配“never”中的“er”。
\cx
匹配有x指明的控制字符。例如,\cM匹配一个Control-M或回车符。x的值必须为A-Z或a-z之一。否则,将c视为一个原义的“c”字符。
\d
匹配一个数字字符。等价于[0-9]。
\D
匹配一个非数字字符。等价于[^0-9]。
\f
匹配一个换页符。等价于\x0c和\cL。
\n
匹配一个换行符。等价于\x0a和\cJ。
\r
匹配一个回车符。等价于\x0d和\cM。
\s
匹配任何空白字符,包括空格、制表符、换页符等等。等价于[ \f\n\r\t\v]。
\S
匹配任何非空白字符。等价于[^ \f\n\r\t\v]。
\t
匹配一个制表符。等价于\x09和\cI。
\v
匹配一个垂直制表符。等价于\x0b和\cK。
\w
匹配包括下划线的任何单词字符。等价于“[A-Za-z0-9_]”。
\W
匹配任何非单词字符。等价于“[^A-Za-z0-9_]”。
\xn
匹配n,其中n为十六进制转义值。十六进制转义值必须为确定的两个数字长。例如,“\x41”匹配“A”。“\x041”则等价于“\x04&1”。正则表达式中可以使用ASCII编码。.
\num
匹配num,其中num是一个正整数。对所获取的匹配的引用。例如,“(.)\1”匹配两个连续的相同字符。
\n
标识一个八进制转义值或一个向后引用。如果\n之前至少n个获取的子表达式,则n为向后引用。否则,如果n为八进制数字(0-7),则n为一个八进制转义值。
\nm
标识一个八进制转义值或一个向后引用。如果\nm之前至少有nm个获得子表达式,则nm为向后引用。如果\nm之前至少有n个获取,则n为一个后跟文字m的向后引用。如果前面的条件都不满足,若n和m均为八进制数字(0-7),则\nm将匹配八进制转义值nm。
\nml
如果n为八进制数字(0-3),且m和l均为八进制数字(0-7),则匹配八进制转义值nml。
\un
匹配n,其中n是一个用四个十六进制数字表示的Unicode字符。例如,\u00A9匹配版权符号(©)。
常用正则表达式
用户名
/^[a-z0-9_-]{3,16}$/
密码
/^[a-z0-9_-]{6,18}$/
十六进制值
/^#?([a-f0-9]{6}
[a-f0-9]{3})$/
电子邮箱
/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/
/^[a-z\d]+(\.[a-z\d]+)*@([\da-z](-[\da-z])?)+(\.{1,2}[a-z]+)+$/
URL
/^(https?://)?([\da-z\.-]+)\.([a-z\.]{2,6})([/\w \.-]*)*/?$/
IP 地址
/((2[0-4]\d
25[0-5]
[01]?\d\d?)\.){3}(2[0-4]\d
25[0-5]
[01]?\d\d?)/
/^(?:(?:25[0-5]
2[0-4][0-9]
[01]?[0-9][0-9]?)\.){3}(?:25[0-5]
2[0-4][0-9]
[01]?[0-9][0-9]?)$/
HTML 标签
/^<([a-z]+)([^<]+)*(?:>(.*)</\1>
\s+/>)$/
删除代码\\注释
(?<!http:
\S)//.*$
Unicode编码中的汉字范围
/^[\u2E80-\u9FFF]+$/
1.普通字符
普通字符主要讲解以下内容,并举例说明
// String regStr = "[a-z]";//匹配a-z中任意一个字符// String regStr = "[A-Z]";//匹配A-Z中任何一个字符// String regStr = "abc";//匹配字符串abc// String regStr = "(?i)abc";//匹配字母abc不区分大小写// String regStr = "[0-9]";//匹配0-9任何一个字符// String regStr = "[^0-9]";//匹配不是0-9中的任何一个字符// String regStr = "[^0-9]{2}";//匹配2个不是0-9的字符// String regStr = "\\d";//匹配任何一个数字字符,等价于[0-9]// String regStr = "\\D";//匹配任何一个非数字字符,等价于[^0-9]// String regStr = "\\w";//匹配任何一个数字、字母、下划线,等价于[0-9a-zA-Z_]// String regStr = "\\W";//匹配任何一个除了数字、字母、下划线,等价于[^0-9a-zA-Z_]// String regStr = "\\s";//匹配任何一个空字符// String regStr = "\\S";//匹配任何一个非空字符// String regStr = "ab
cd";//选择匹配符,匹配字符串ab或者cd1) String regStr = "[a-z]";//匹配a-z中任意一个字符
@Test public void test1() { String str = "abc2021"; String regStr = "[a-z]"; Pattern compile = Pattern.compile(regStr); Matcher matcher = compile.matcher(str); while(matcher.find()){ System.out.println("匹配到的数据为:"+matcher.group(0)); } }
结果展示
2) String regStr = "[A-Z]";//匹配A-Z中任何一个字符
@Testpublic void test2(){ String str = "ABCabc2021"; String regStr = "[A-Z]"; Pattern compile = Pattern.compile(regStr); Matcher matcher = compile.matcher(str); while(matcher.find()){ System.out.println("匹配到的数据为:"+matcher.group(0)); }}
结果展示
3)String regStr = "abc";//匹配字符串abc
@Testpublic void test2(){ String str = "ABCabc2021"; String regStr = "abc"; Pattern compile = Pattern.compile(regStr); Matcher matcher = compile.matcher(str); while(matcher.find()){ System.out.println("匹配到的数据为:"+matcher.group(0)); }}
结果展示
4)String regStr = "(?i)abc";//匹配字母abc不区分大小写
@Testpublic void test2(){ String str = "ABCabc2021"; String regStr = "(?i)abc"; Pattern compile = Pattern.compile(regStr); Matcher matcher = compile.matcher(str); while(matcher.find()){ System.out.println("匹配到的数据为:"+matcher.group(0)); }}
结果展示
5) String regStr = "[0-9]";//匹配0-9任何一个字符
@Testpublic void test2(){ String str = "ABCabc2021"; String regStr = "[0-9]"; Pattern compile = Pattern.compile(regStr); Matcher matcher = compile.matcher(str); while(matcher.find()){ System.out.println("匹配到的数据为:"+matcher.group(0)); }}
结果展示
6) String regStr = "[^0-9]";//匹配不是0-9中的任何一个字符
@Testpublic void test2(){ String str = "ABCabc2021"; String regStr = "[^0-9]"; Pattern compile = Pattern.compile(regStr); Matcher matcher = compile.matcher(str); while(matcher.find()){ System.out.println("匹配到的数据为:"+matcher.group(0)); }}结果展示
2.限定符
/** * 限定符 * *:表示出现任意次数,0次或者n次,如(abc)*表示abc出现0次或者多次 * +:表示出现至少1次或者n次,如(abc)+表示abc出现1次或者多次 * ?:表示出现至少0次或者1次,如abc?表示c出现0次或者1次 * {n}:表示出现n次,如[0-9]{2},表示匹配2次数字 * {n,}表示至少出现n次,如[0-9]{3,}表示匹配至少3次数字 * {n,m}表示出现至少n次,最多m次,如[0-9]{2,4}表示匹配次数2-4次数字 */1) *:表示出现任意次数,0次或者n次
@Testpublic void test2(){ String str = "zypabcabc2021"; String regStr = "zyp(abc)*"; Pattern compile = Pattern.compile(regStr); Matcher matcher = compile.matcher(str); while(matcher.find()){ System.out.println("匹配到的数据为:"+matcher.group(0)); }}
结果展示
2)+:表示出现至少1次或者n次,如(abc)+表示abc出现1次或者多次
@Testpublic void test2(){ String str = "zypabc2021"; String regStr = "zyp(abc)+"; Pattern compile = Pattern.compile(regStr); Matcher matcher = compile.matcher(str); while(matcher.find()){ System.out.println("匹配到的数据为:"+matcher.group(0)); }}
结果展示
3)?:表示出现至少0次或者1次,如abc?表示c出现0次或者1次
@Testpublic void test2(){ String str = "zyp2021"; String regStr = "zyp(abc)?"; Pattern compile = Pattern.compile(regStr); Matcher matcher = compile.matcher(str); while(matcher.find()){ System.out.println("匹配到的数据为:"+matcher.group(0)); }}
结果展示
4){n}:表示出现n次,如[0-9]{2},表示匹配2次数字
@Testpublic void test2(){ String str = "zyp2021"; String regStr = "[0-9]{2}"; Pattern compile = Pattern.compile(regStr); Matcher matcher = compile.matcher(str); while(matcher.find()){ System.out.println("匹配到的数据为:"+matcher.group(0)); }}
结果展示
5){n,}表示至少出现n次,如[0-9]{3,}表示匹配至少3次数字
@Testpublic void test2(){ String str = "zyp2021"; String regStr = "[0-9]{2,}"; Pattern compile = Pattern.compile(regStr); Matcher matcher = compile.matcher(str); while(matcher.find()){ System.out.println("匹配到的数据为:"+matcher.group(0)); }}
结果展示
6){n,m}表示出现至少n次,最多m次,如[0-9]{2,4}表示匹配次数2-4次数字
@Testpublic void test2(){ String str = "zyp2021"; String regStr = "[0-9]{2,4}"; Pattern compile = Pattern.compile(regStr); Matcher matcher = compile.matcher(str); while(matcher.find()){ System.out.println("匹配到的数据为:"+matcher.group(0)); }}
结果展示
3.定位符
/** * 定位符 * ^:表示字符串以什么开头的意思。如:有一个字符串123abc,正则为^[0-9]+[a-z]*(必须已数字开头),则能成功匹配上。如果字符串为a123abc则匹配不上 * $:表示字符串以什么结束的意思。如:有一个字符串123abc,正则为^[0-9]+[a-z]+$(表示以数字开头,字母结尾)则能成功匹配上。如果字符串为a123abc1则匹配不上 * \\b:表示边缘匹配(字符串的结尾或者空格之后)。有一个字符串abc123abc,正则为abc\\b,匹配到的为最后的那个abc * \\B:与\\b相反 */1) ^:表示字符串以什么开头的意思
@Testpublic void test2(){ String str = "2021zyp"; String regStr = "^[0-9]+"; Pattern compile = Pattern.compile(regStr); Matcher matcher = compile.matcher(str); while(matcher.find()){ System.out.println("匹配到的数据为:"+matcher.group(0)); }}
结果展示
2) $:表示字符串以什么结束的意思
@Testpublic void test2(){ String str = "2021zyp"; String regStr = "[0-9]#34;; Pattern compile = Pattern.compile(regStr); Matcher matcher = compile.matcher(str); while(matcher.find()){ System.out.println("匹配到的数据为:"+matcher.group(0)); }}
没有匹配到,因为要以数字结束
3) \\b:表示边缘匹配(字符串的结尾或者空格之后)
@Testpublic void test2(){ String str = "zyp2021zyp"; String regStr = "zyp\\b"; Pattern compile = Pattern.compile(regStr); Matcher matcher = compile.matcher(str); while(matcher.find()){ System.out.println("匹配到的数据为:"+matcher.group(0)); }}
匹配到的是最后一个“zyp”
4) \\B:与\\b相反
@Testpublic void test2(){ String str = "zyp2021zyp"; String regStr = "zyp\\B"; Pattern compile = Pattern.compile(regStr); Matcher matcher = compile.matcher(str); while(matcher.find()){ System.out.println("匹配到的数据为:"+matcher.group(0)); }}
匹配到的是第一个“zyp”
4.分组
/** * 分组:可分为捕获分组和非捕获分组 * 1.捕获分组: * 1)如(\\d\\d)(\\d\\d)表示匹配4位数字,如果字符串位2021abcd, * 我们通过matcher.group(0)得到2021 * 通过matcher.group(1)得到20 * 通过matcher.group(2)得到21 * 由此可见()起到分组的作用 * * 2)如(?<a1>\\d\\d)(?<a2>\\d\\d)表示匹配4位数字,如果字符串位2021abcd, * 我们通过matcher.group(0)得到2021 * 通过matcher.group(1)得到20,还可以通过matcher.group(a1)得到20 * 通过matcher.group(2)得到21,还可以通过matcher.group(a2)得到21 * 由此可见()起到分组的作用 * * 2.非捕获分组:不能通过group(1)或者group(2)获取值 * 1)如20(?:20|21|22)表示匹配2020|2021|2022 * 2) 如20(?=20
21
22)表示匹配2020或2021或2022中的20 * 3)如20(?!20
21
22)表示匹配不匹配2020或2021或2022中的20,匹配其它20 * */捕获分组
1)如(\\d\\d)(\\d\\d)表示匹配4位数字,如果字符串为2021abcd,
@Testpublic void test2(){ String str = "2021abcd"; String regStr = "(\\d\\d)(\\d\\d)"; Pattern compile = Pattern.compile(regStr); Matcher matcher = compile.matcher(str); while(matcher.find()){ System.out.println("matcher.group(0):"+matcher.group(0)); System.out.println("分组一:matcher.group(1):"+matcher.group(1)); System.out.println("分组二:matcher.group(2):"+matcher.group(2)); }}
结果展示
结论:由此可见()会将正则分组,并按顺序给出编号,从1开始
2)(?<a1>\\d\\d)(?<a2>\\d\\d)表示匹配4位数字,如果字符串位2021abcd
@Testpublic void test2(){ String str = "2021abcd"; String regStr = "(?<a1>\\d\\d)(?<a2>\\d\\d)"; Pattern compile = Pattern.compile(regStr); Matcher matcher = compile.matcher(str); while(matcher.find()){ System.out.println("matcher.group(0):"+matcher.group(0)); System.out.println("分组一:matcher.group(1):"+matcher.group(1)); System.out.println("分组二:matcher.group(2):"+matcher.group(2)); System.out.println("分组名a1:matcher.group(1):"+matcher.group("a1")); System.out.println("分组名a2:matcher.group(2):"+matcher.group("a2")); }}
结果展示
结论:由此可见()除了能将正则分组,还能按顺序给出编号,从1开始。还可以给分组取名字,并根据名字获取对应匹配的值
非捕获分组
1)如20(?:20|21|22)表示匹配2020|2021|2022
@Testpublic void test2(){ String str = "2021a2022B2023"; String regStr = "20(?:20
21
22)"; Pattern compile = Pattern.compile(regStr); Matcher matcher = compile.matcher(str); while(matcher.find()){ System.out.println("匹配到的数据为:"+matcher.group(0)); }}
结果展示
2)如20(?=20
21
22)表示匹配2020或2021或2022中的20
@Testpublic void test2(){ String str = "2021a2022B2023"; String regStr = "20(?=20
21
22)"; Pattern compile = Pattern.compile(regStr); Matcher matcher = compile.matcher(str); while(matcher.find()){ System.out.println("匹配到的数据为:"+matcher.group(0)); }}
这里匹配到的20,为2021和2022中的20
3)如20(?!20
21
22)表示匹配不匹配2020或2021或2022中的20,匹配其它20
@Testpublic void test2(){ String str = "2021a2022B2023"; String regStr = "20(?!20
21
22)"; Pattern compile = Pattern.compile(regStr); Matcher matcher = compile.matcher(str); while(matcher.find()){ System.out.println("匹配到的数据为:"+matcher.group(0)); }}
这里匹配到的20为2023中的20
5.反向引用
/** * 反向引用 * 如果我们要找到一个字符串中连续4位威数字,并且第一位和第4位要相同,第二位和第三位相同。 * 这时候我们使用反向引用就很简单 * 反向引用的内部用法:\\n其中n代表分组号,如:字符串12345678870008,正则为(\\d)(\\d)\\2\\1 * 反向引用的外部用法:$n其中n代表分组号 */字符串12345678870008,正则为(\\d)(\\d)\\2\\1
@Test public void test2(){ String str = "12345678870008"; /** * 第一个(\\d)会分配的组为1 * 第2个(\\d)会分配的组为2 * \\2:表示引用组2的值,因此2和3的值就会相同 * \\1:表示引用组1的值,因此1和4的值会相同 */ String regStr = "(\\d)(\\d)\\2\\1"; Pattern compile = Pattern.compile(regStr); Matcher matcher = compile.matcher(str); while(matcher.find()){ System.out.println("匹配到的数据为:"+matcher.group(0)); } }
结果展示