正则表达式
正则表达式(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)?" 可以匹配 "do" 或 "does" 。? 等价于 {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、\r)之外的任何单个字符。要匹配包括 '\n' 在内的任何字符,请使用像"(.|\n)"的模式。 |
(pattern) |
匹配 pattern 并获取这一匹配。所获取的匹配可以从产生的 Matches 集合得到,在VBScript 中使用 SubMatches 集合,在JScript 中则使用 $0…$9 属性。要匹配圆括号字符,请使用 '\(' 或 '\)'。 |
(?:pattern) |
匹配 pattern 但不获取匹配结果,也就是说这是一个非获取匹配,不进行存储供以后使用。这在使用 "或" 字符 (|) 来组合一个模式的各个部分是很有用。例如, 'industr(?:y|ies) 就是一个比 'industry|industries' 更简略的表达式。 |
(?=pattern) |
正向肯定预查(look ahead positive assert),在任何匹配pattern的字符串开始处匹配查找字符串。这是一个非获取匹配,也就是说,该匹配不需要获取供以后使用。例如,"Windows(?=95|98|NT|2000)"能匹配"Windows2000"中的"Windows",但不能匹配"Windows3.1"中的"Windows"。预查不消耗字符,也就是说,在一个匹配发生后,在最后一次匹配之后立即开始下一次匹配的搜索,而不是从包含预查的字符之后开始。 |
(?!pattern) |
正向否定预查(negative assert),在任何不匹配pattern的字符串开始处匹配查找字符串。这是一个非获取匹配,也就是说,该匹配不需要获取供以后使用。例如"Windows(?!95|98|NT|2000)"能匹配"Windows3.1"中的"Windows",但不能匹配"Windows2000"中的"Windows"。预查不消耗字符,也就是说,在一个匹配发生后,在最后一次匹配之后立即开始下一次匹配的搜索,而不是从包含预查的字符之后开始。 |
(?<=pattern) | 反向(look behind)肯定预查,与正向肯定预查类似,只是方向相反。例如,"(?<=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'、'l'、'i'、'n'。 |
[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 匹配版权符号 (?)。 |
正则表达式 - 运算符优先级
正则表达式从左到右进行计算,并遵循优先级顺序,这与算术表达式非常类似。
相同优先级的从左到右进行运算,不同优先级的运算先高后低。下表从最高到最低说明了各种正则表达式运算符的优先级顺序:
运算符 | 描述 |
---|---|
\ | 转义符 |
(), (?:), (?=), [] | 圆括号和方括号 |
*, +, ?, {n}, {n,}, {n,m} | 限定符 |
^, $, \任何元字符、任何字符 | 定位点和序列(即:位置和顺序) |
| | 替换,"或"操作 字符具有高于替换运算符的优先级,使得"m|food"匹配"m"或"food"。若要匹配"mood"或"food",请使用括号创建子表达式,从而产生"(m|f)ood"。 |
字符范围:
[a-z] //匹配所有的小写字母
[A-Z] //匹配所有的大写字母
[a-zA-Z] //匹配所有的字母
[0-9] //匹配所有的数字
[0-9\.\-] //匹配所有的数字,句号和减号
[ \f\r\t\n] //匹配所有的白字符
代码示例:
package 正则表达式;
//**也叫规则表达式
//正则表达式是对字符串
//(包括普通字符(例如,a 到 z 之间的字母)
//和特殊字符(称为“元字符”))操作的一种逻辑公式,
//就是用事先定义好的一些特定字符、及这些特定字符的组合,
//组成一个“规则字符串”,这个“规则字符串”用来表达对字符串的一种过滤逻辑。正则表达式是一种文本模式,
//该模式描述在搜索文本时要匹配的一个或多个字符串。*/
import org.junit.Test;
public class RegxTest {
@Test
public void Test1() {
System.out.println("Hello");
}
/**
* 正则表达式规范
*/
@Test
public void Test2() {
String regx = "\\d{11}";
String str = "13312131231";
System.out.println(regx.matches(regx));
}
/**
* String regx = "Hello";该规则代表仅匹配Hello
*/
@Test
public void test3() {
String regx = "Hello";
String str = "Hello";
System.out.println(str.matches(regx));
}
/**
* String regx = "Hello";该规则代表仅匹配Hello
*/
@Test
public void test4() {
String regx = "[a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9]lo";
String str = "hello";
System.out.println(str.matches(regx));
str = "aello";
System.out.println(str.matches(regx));
str = "2aello";
System.out.println(str.matches(regx));
}
/**
*
* + 匹配一次到多次;
* * 匹配一次到多次;
* ? 匹配重复的次数;
*/
@Test
public void test5() {
// String regx = "[a-zA-Z0-9]+";
// String regx = "[a-zA-Z0-9]*";
String regx = "[a-zA-Z0-9]?";
String str = "hello";
System.out.println(str.matches(regx));
str = "aello";
System.out.println(str.matches(regx));
str = "2aello";
System.out.println(str.matches(regx));
str = "a";
System.out.println(str.matches(regx));
str = "2a@llo";
System.out.println(str.matches(regx));
}
/**
* {3} 匹配3次
* {3,}匹配3次以上
* {3,5}匹配3到5次
*/
@Test
public void test6() {
// String regx = "[a-zA-Z0-9]+";
// String regx = "[a-zA-Z0-9]*";
String regx = "[a-zA-Z0-9] {3}";
String str = "hello";
System.out.println(str.matches(regx));
str = "hel";
System.out.println(str.matches(regx));
str = "he";
System.out.println(str.matches(regx));
System.out.println("*******************");
regx = "[a-zA-Z0-9] {3,}";
str = "hello";
System.out.println(str.matches(regx));
str = "hel";
System.out.println(str.matches(regx));
str = "he";
System.out.println(str.matches(regx));
System.out.println("*******************");
regx = "[a-zA-Z0-9] {3,5}";
str = "hello";
System.out.println(str.matches(regx));
str = "hel";
System.out.println(str.matches(regx));
str = "he";
System.out.println(str.matches(regx)); System.out.println(str.matches(regx));
}
/**
* String regx = "Hello";该规则代表仅匹配Hello
*/
@Test
public void test7() {
String regx = "[^0-9]{3}";
String str = "0el";
System.out.println(str.matches(regx));
str = "@el";
System.out.println(str.matches(regx));
}
/**
* \\d表示匹配数字
* \\D不匹配数字
* \\w匹配所有字符相当于[a-zA-Z0-9]
* \\W不匹配所有字符相当于[^a-zA-Z0-9]
* \\s匹配任何空格字符,包括空格,制表,分页字符
* \\S不匹配任何空格字符,包括空格,制表,分页字符
* .表示匹配任意字符
*/
@Test
public void test8() {
String regx = "\\d{3}";
String str = "032";
System.out.println(str.matches(regx));
str = "@21";
System.out.println(str.matches(regx));
System.out.println("****************");
regx = "\\D{3}";
str = "0el";
System.out.println(str.matches(regx));
str = "@el";
System.out.println(str.matches(regx));
System.out.println("****************");
regx = "\\w{3}";
str = "032";
System.out.println(str.matches(regx));
str = "@e";
System.out.println(str.matches(regx));
regx = "\\W{3}";
str = "032";
System.out.println(str.matches(regx));
str = "@e1213ad";
System.out.println(str.matches(regx));
System.out.println("****************");
regx = "\\s{3}";
str = "032 ";
System.out.println(str.matches(regx));
str = " @e";
System.out.println(str.matches(regx));
System.out.println("****************");
regx = "\\S{3}";
str = "0 32";
System.out.println(str.matches(regx));
str = "@e";
System.out.println(str.matches(regx));
System.out.println("****************");
regx = ".{3}";
str = "0 32";
System.out.println(str.matches(regx));
str = "@e";
System.out.println(str.matches(regx));
}
/**
*
* 转义符
* \\代表一个斜杠\
* \/代表一个/
* \[匹配一个[
* \]匹配一个]
* \.匹配一个.
* \{匹配一个{
* \}匹配一个}
* ......
*/
@Test
public void test9() {
String regx = "\\\\\\/\\[\\]\\.";
String str = "\\/[].";
System.out.println(str.matches(regx));
}
/**
* 分组和或
*
*
*/
@Test
public void test10() {
String regx = "\\(0\\d{2}\\)|0\\d{2}( |\\-|_)\\d{7,8}";
String str = "029 2342332";
System.out.println(str.matches(regx));
}
}