Java正则

原创:转载需注明原创地址 https://www.cnblogs.com/fanerwei222/p/11848407.html

Java正则的编写和使用:

使用方法:

/**
* 正则表达式字符串
*/
 String patternString = "zoux*";
/**
* 需要匹配验证的字符串
*/
String needPatString = "z00000000";
/**
* 规则匹配对象
*/
Pattern pattern = Pattern.compile(patternString);
/**
* 使用规则匹配对象去验证字符串是否匹配正则
*/
pattern.matcher(needPatString).matches();
/**
* 直接用字符串中的matches()方法进行匹配效果一样
*/
needPatString.matches(patternString);

1. 的用法: 特殊字符或者转义用的:例如


 表示换行 (特殊符号)

\ 表示匹配  这个符号 (转义)

\\ 表示匹配 \ 这个符号 (转义)

\( 表示匹配 ( 这个符号 (转义)
System.out.println("(".matches("\(")); // true
System.out.println("(xx".matches("\(")); // false
System.out.println("\".matches("\\")); // true

2. ^ 的用法: ^ 表示以它后面的表达式元素开头, 边界符.例如:

^d+ 表示第一个开头的必须是数字, 如果是别的就无法匹配成功

d+   表示匹配数字类型

有 a1234567 , 去匹配 ^d+ 则匹配失败, 因为第一个元素是a, 属于字母, 如果匹配 d+ 则匹配成功!

3. $ 的用法: $表示以它前面的表达式元素为结尾, 边界符. 例如:

d+$ 表示最后一个元素必须是数字, 如果是别的就无法匹配成功

d+   表示匹配数字类型

有 1234567b , 去匹配 d+$ 则匹配失败, 因为最后一个元素是b, 属于字母, 如果匹配 d+ 则匹配成功!

4. * 的用法: 对长度为n的表达式(包括 * 在内), 首先匹配其下标为 0 到  n-3 的字符串, 然后对下标为n-2的字符串进行 0次 或 1次 或 有限多次的匹配! 比较拗口, 来看例子:

/**
* 我们的正则表达式 长度为5
* 下标 0 到 下标n-3 对应 --> zou , 首先匹配 zou, 是 z 或者 zo 都不行, 一定要是 zou
* 下标为 n-2 的字符串是 --> x, 在上面规则的基础上对 x 进行 0次 或 1次 或 有限多次的匹配
*/
String regex = "zoux*";
String le0 = "z";
String le1 = "zo";
String le2 = "zoo";
String le3 = "uzoo";
String le4 = "zouxxx";
String le5 = "zoux";
String le6 = "xxxx";
String le7 = "zou";
System.out.println(le0.matches(regex));
System.out.println(le1.matches(regex));
System.out.println(le2.matches(regex));
System.out.println(le3.matches(regex));
System.out.println(le4.matches(regex));
System.out.println(le5.matches(regex));
System.out.println(le6.matches(regex));
System.out.println(le7.matches(regex));

打印结果如下:

false
false
false
false
true
true
false
true

 5. + 的用法: 对长度为n的表达式(包括 + 在内), 首先匹配其下标为 0 到  n-3 的字符串, 然后对下标为n-2的字符串进行 1次 或 有限多次的匹配! 比较拗口, 来看例子:

String regex = "zoux+";
String le0 = "z";
String le1 = "zo";
String le2 = "zoo";
String le3 = "uzoo";
String le4 = "zouxxx";
String le5 = "zoux";
String le6 = "xxxx";
String le7 = "zou";
System.out.println(le0.matches(regex));
System.out.println(le1.matches(regex));
System.out.println(le2.matches(regex));
System.out.println(le3.matches(regex));
System.out.println(le4.matches(regex));
System.out.println(le5.matches(regex));
System.out.println(le6.matches(regex));
System.out.println(le7.matches(regex));

打印结果如下:

false
false
false
false
true
true
false
false

6. ? 的用法: 匹配表达式或子表达式 0次 或 1次, 直接看例子:

String regex = "zoux?";
String le0 = "z";
String le1 = "zo";
String le2 = "zoo";
String le3 = "uzoo";
String le4 = "zouxxx";
String le5 = "zoux";
String le6 = "xxxx";
String le7 = "zou";
System.out.println(le0 + ": " + le0.matches(regex));
System.out.println(le1 + ": " + le1.matches(regex));
System.out.println(le2 + ": " + le2.matches(regex));
System.out.println(le3 + ": " + le3.matches(regex));
System.out.println(le4 + ": " + le4.matches(regex));
System.out.println(le5 + ": " + le5.matches(regex));
System.out.println(le6 + ": " + le6.matches(regex));
System.out.println(le7 + ": " + le7.matches(regex));

打印如下:

z: false
zo: false
zoo: false
uzoo: false
zouxxx: false
zoux: true
xxxx: false
zou: true

 再来一个:

String regex = "zo(ux)?";
String le0 = "z";
String le1 = "zo";
String le2 = "zoo";
String le3 = "uzoo";
String le4 = "zouxxx";
String le5 = "zoux";
String le6 = "xxxx";
String le7 = "zou";
String le8 = "zouxuxux";
System.out.println(le0 + ": " + le0.matches(regex));
System.out.println(le1 + ": " + le1.matches(regex));
System.out.println(le2 + ": " + le2.matches(regex));
System.out.println(le3 + ": " + le3.matches(regex));
System.out.println(le4 + ": " + le4.matches(regex));
System.out.println(le5 + ": " + le5.matches(regex));
System.out.println(le6 + ": " + le6.matches(regex));
System.out.println(le7 + ": " + le7.matches(regex));
System.out.println(le8 + ": " + le8.matches(regex));

 打印如下:

z: false
zo: true
zoo: false
uzoo: false
zouxxx: false
zoux: true
xxxx: false
zou: false
zouxuxux: false

 7. {n} 的用法: n是非负整数, {n} 表示正好匹配 n 次, 例子:

String regex = "o{4}";
String str1 = "fod";
String str2 = "food";
String str3 = "foood";
String str4 = "fooood";
String str5 = "oooo";
String str6 = "ooo";
System.out.println(str1 + ": " + str1.matches(regex));
System.out.println(str2 + ": " + str2.matches(regex));
System.out.println(str3 + ": " + str3.matches(regex));
System.out.println(str4 + ": " + str4.matches(regex));
System.out.println(str5 + ": " + str5.matches(regex));
System.out.println(str6 + ": " + str6.matches(regex));

 打印如下:

fod: false
food: false
foood: false
fooood: false
oooo: true
ooo: false

 8. {n,} 的用法(逗号后面不能有空格): n为非负整数, {n,} 表示至少匹配 n次, 例如:

String regex = "o{4,}";
String str1 = "fod";
String str2 = "food";
String str3 = "foood";
String str4 = "fooood";
String str5 = "ooooo";
String str6 = "oooo";
String str7 = "ooo";
System.out.println(str1 + ": " + str1.matches(regex));
System.out.println(str2 + ": " + str2.matches(regex));
System.out.println(str3 + ": " + str3.matches(regex));
System.out.println(str4 + ": " + str4.matches(regex));
System.out.println(str5 + ": " + str5.matches(regex));
System.out.println(str6 + ": " + str6.matches(regex));

 打印如下:

fod: false
food: false
foood: false
fooood: false
ooooo: true
oooo: true

 9. {n,m} 的用法: 如上, n < m, n, m为非负整数, {n,m} 表示匹配 至少 n次, 至多 m次, 例子省略.

10. ?的非贪心用法: ? 在紧随任何其他限定符例如--> *、+、{n}、{n,}、{n,m} 这些之后, 匹配模式就变成了 "非贪心的", 就是匹配的尽可能少, 只要匹配到了就收手! 看例子:

在字符串"oooo"中,"o+?"只匹配单个"o",而"o+"匹配所有"o"

11. "." 的用法: 匹配除" "之外的任何单个字符:

String regex = ".";
String str1 = "f";
String str2 = "o";
String str3 = "d";
String str4 = "f";
System.out.println(str1 + ": " + str1.matches(regex));
System.out.println(str2 + ": " + str2.matches(regex));
System.out.println(str3 + ": " + str3.matches(regex));
System.out.println(str4 + ": " + str4.matches(regex));

打印如下:

f: true
o: true
d: true
f: true

12. () 子表达式用法: 把 () 中的内容看做一个整体. 这玩意叫 "捕获匹配" , 啥意思呢, 我说明一下 :

String str = "8000$";
String str1 = "4999¥";
/**
 * 此正则表达式的目的是捕获 数字 + 人民币/美元 的这种形式的字符串
 */
String patternStr = "(\d+)([$¥])";
Pattern pattern = Pattern.compile(patternStr);
Matcher matcher = pattern.matcher(str);
if (matcher.matches()) {
    System.out.println(matcher.group());
    System.out.println(matcher.group(0));
    System.out.println(matcher.group(1));
    System.out.println(matcher.group(2));
}

 打印如下:

8000$
8000$
8000
$

 括号括起来的就是一个捕获组, 然后可以在匹配到了之后通过下标打印出来, 捕获组 (\d+) 打印的是 8000 这些数字, ([$¥]) 打印的是 $ 这个美元符号. 这个跟下面那个非捕获组密切相关.

13. (?:) 非捕获组用法:  在上面基础上增加一些内容:

String str = "8000.98$"; //此处将str改为有小数的字符串了
String str1 = "4999¥";
/**
 * 此正则表达式的目的是捕获 数字 + 人民币/美元 的这种形式的字符串
 */
String patternStr = "(\d+)(\.)(\d+)([$¥])";
Pattern pattern = Pattern.compile(patternStr);
Matcher matcher = pattern.matcher(str);
if (matcher.matches()) {
    System.out.println(matcher.group());
    System.out.println(matcher.group(0));
    System.out.println(matcher.group(1));
    System.out.println(matcher.group(2));
}

 打印如下:

8000.98$
8000.98$
8000
.

 此时这里就对不上了, 因为现在有4个捕获组, 所以可以group从1到4, 如果我们不想修改下面打印的代码但是我们还想打印整数和符号部分怎么办呢? 非捕获组就派上用场了!

String str = "8000.98$";//此处将str改为有小数的字符串了
String str1 = "4999¥";
/**
 * 此正则表达式的目的是捕获 数字 + 人民币/美元 的这种形式的字符串
 */
String patternStr = "(\d+)(?:\.)(?:\d+)([$¥])";
Pattern pattern = Pattern.compile(patternStr);
Matcher matcher = pattern.matcher(str);
if (matcher.matches()) {
    System.out.println(matcher.group());
    System.out.println(matcher.group(0));
    System.out.println(matcher.group(1));
    System.out.println(matcher.group(2));
}

打印如下;

8000.98$
8000.98$
8000
$

这就是非捕获组!

14. (?=pattern) 正向预测匹配搜索, 这个容易理解, 直接搬人家的解释吧 :

例如,'Windows (?=95|98|NT|2000)' 匹配"Windows 2000"中的"Windows",但不匹配"Windows 3.1"中的"Windows"

 15. (?!pattern) 反向预测匹配搜索, 这个和上一个相反, 我就直接搬人家的解释了:

例如,'Windows (?!95|98|NT|2000)' 匹配"Windows 3.1"中的 "Windows",但不匹配"Windows 2000"中的"Windows"

 16. x|y , 或的意思, 容易理解:

匹配 x 或 y。例如,'z|food' 匹配"z"或"food"。'(z|f)ood' 匹配"zood"或"food"。

 17. [xyz] , 之一的意思, 容易理解:

String regex = "[xyz]";
String str1 = "x";
String str2 = "y";
String str3 = "z";
String str4 = "k";
System.out.println(str1 + ": " + str1.matches(regex));
System.out.println(str2 + ": " + str2.matches(regex));
System.out.println(str3 + ": " + str3.matches(regex));
System.out.println(str4 + ": " + str4.matches(regex));

 看打印, 懂了吧

x: true
y: true
z: true
k: false

 18. [^xyz], 和上面那个相反, 中括号里面的都不匹配, 来个代码吧:

String regex = "[^xyz]";
String str1 = "x";
String str2 = "y";
String str3 = "z";
String str4 = "k";
System.out.println(str1 + ": " + str1.matches(regex));
System.out.println(str2 + ": " + str2.matches(regex));
System.out.println(str3 + ": " + str3.matches(regex));
System.out.println(str4 + ": " + str4.matches(regex));

 看打印, 一目了然

x: false
y: false
z: false
k: true

 19. [a-z] 字符范围的意思, 从a到z, 根据ASCII码顺序来的吧应该, 我猜的:

String regex = "[a-z]";
String str1 = "x";
String str2 = "y";
String str3 = "z";
String str4 = "k";
System.out.println(str1 + ": " + str1.matches(regex));
System.out.println(str2 + ": " + str2.matches(regex));
System.out.println(str3 + ": " + str3.matches(regex));
System.out.println(str4 + ": " + str4.matches(regex));

看打印, 果然如此

x: true
y: true
z: true
k: true

20. [^a-z] 反向范围的意思, 来看代码吧, 说多了好累:

String regex = "[^a-z]";
String str1 = "x";
String str2 = "y";
String str3 = "z";
String str4 = "k";
System.out.println(str1 + ": " + str1.matches(regex));
System.out.println(str2 + ": " + str2.matches(regex));
System.out.println(str3 + ": " + str3.matches(regex));
System.out.println(str4 + ": " + str4.matches(regex));

打印在此

x: false
y: false
z: false
k: false

下面的内容比较恶心, 都是反斜杠组成的各种英文字母代表各种意思, 我觉得十分恶心, 哪怕是单词也好呀, 哎哎. 继续写吧. 如果是那啥 换行符/操作符/啥啥啥符号的我就不解释了, 我也懒得用这玩意, 就解释一些平时用得着的. 那玩意实在太恶心, 我记不住.

1. d 数字字符匹配。等效于 [0-9]。

2. D 非数字字符匹配。等效于 [^0-9]。

3.  一个单词的边界字符匹配, "er"匹配"never"中的"er",但不匹配"verb"中的"er"

4. B 非字边界匹配, "erB"匹配"verb"中的"er",但不匹配"never"中的"er"

5. 
 换行符, 谁都知道吧.等效于 x0a 和 cJ。

6. s 空白字符, 包括空格、制表符、换页符等。与 [ f

	v] 等效。

7. S 非空白字符, 与 [^ f

	v] 等效。

8. w 匹配任何字类字符,包括下划线。与"[A-Za-z0-9_]"等效

9. W 与任何非单词字符匹配。与"[^A-Za-z0-9_]"等效

下面这些我不知道啥玩意:

cx 匹配 x 指示的控制字符。例如,cM 匹配 Control-M 或回车符。x 的值必须在 A-Z 或 a-z 之间。如果不是这样,则假定 c 就是"c"字符本身。
f 换页符匹配。等效于 x0c 和 cL。

 匹配一个回车符。等效于 x0d 和 cM。
	 制表符匹配。与 x09 和 cI 等效。
v 垂直制表符匹配。与 x0b 和 cK 等效。
xn 匹配 n,此处的 n 是一个十六进制转义码。十六进制转义码必须正好是两位数长。例如,"x41"匹配"A"。"x041"与"x04"&"1"等效。允许在正则表达式中使用 ASCII 代码。

um 匹配 num,此处的 num 是一个正整数。到捕获匹配的反向引用。例如,"(.)1"匹配两个连续的相同字符。

 标识一个八进制转义码或反向引用。如果 
 前面至少有 n 个捕获子表达式,那么 n 是反向引用。否则,如果 n 是八进制数 (0-7),那么 n 是八进制转义码。

m 标识一个八进制转义码或反向引用。如果 
m 前面至少有 nm 个捕获子表达式,那么 nm 是反向引用。如果 
m 前面至少有 n 个捕获,则 n 是反向引用,后面跟有字符 m。如果两种前面的情况都不存在,则 
m 匹配八进制值 nm,其中 n 和 m 是八进制数字 (0-7)。

ml 当 n 是八进制数 (0-3),m 和 l 是八进制数 (0-7) 时,匹配八进制转义码 nml。
un 匹配 n,其中 n 是以四位十六进制数表示的 Unicode 字符。例如,u00A9 匹配版权符号 (©)。

下面还有一个特殊说明: Java的规定!

根据 Java Language Specification 的要求,Java 源代码的字符串中的反斜线被解释为 Unicode 转义或其他字符转义。因此必须在字符串字面值中使用两个反斜线,
表示正则表达式受到保护,不被 Java 字节码编译器解释。例如,当解释为正则表达式时,字符串字面值 "" 与单个退格字符匹配,而 "\b" 与单词边界匹配。
字符串字面值 "(hello)" 是非法的,将导致编译时错误;要与字符串 (hello) 匹配,必须使用字符串字面值 "\(hello\)"。

**** 查找所有 ${} 标签中的内容并且替换

String hello1 = "emmmmm";
String world1 = "oooooooooooooo";
Map<String, String> map = new HashMap<>();
map.put("hello1", hello1);
map.put("world1", world1);
String str3 = "good  ${hello1}   this${world1}  nic   e";
String patternStr = "\$\{([^}]*)\}";
Pattern pattern = Pattern.compile(patternStr);
String newStr = str3;
Matcher matcher = pattern.matcher(str3);
while (matcher.find()) {
    System.out.println(matcher.group(1));
    newStr = newStr.replaceAll("\$\{" + matcher.group(1) + "\}", map.get(matcher.group(1)));
}
System.out.println(newStr);

 结束!

原文地址:https://www.cnblogs.com/fanerwei222/p/11848407.html