欢迎进入Java社区论坛,与200万技术人员互动交流 >>进入
四、非匹配
在正则表达式中,我们往往需要在字符串中进行非匹配,这时,就要通过^进行匹配条件限制,^的常见入门用法如下:
[^a-z] 条件限制在非小写a to z范围中一个字符
[^A-Z] 条件限制在非大写A to Z范围中一个字符
[^a-zA-Z] 条件限制在非小写a to z或大写A to Z范围中一个字符
[^0-9] 条件限制在非0 to 9范围中一个字符
[^0-9a-z] 条件限制在非0 to 9或a to z范围中一个字符
代码示例如下:
1 public class RegExp {
2 private Pattern patt;
3 private Matcher matcher;
4
5 public boolean squareReg(String regStr,String regex){
6 return this.commonRegExp(regStr, regex);
7 }
8
9 private boolean commonRegExp(String regStr,String regex){
10 boolean wildcard_Res=false;
11 patt=Pattern.compile(regex);
12 matcher=patt.matcher(regStr);
13 wildcard_Res= matcher.find();
14 return wildcard_Res;
15 }
16 }
17
18 public class TestRegExp {
19 public static void main(String[] args) {
20 RegExp re=new RegExp();
21 boolean wildcard_Res=false;
22
23 wildcard_Res=re.squareReg(“tcn”, “t[^aoe]n”);
24 System.out.println(wildcard_Res);
25 //输出:wildcard_Res=true
26 }
穷则思变,差则思勤!没有比人更高的山没有比脚更长的路。