您的位置:首页 > 编程语言 > Java开发

java正则表达式(四)

2012-10-15 21:16 363 查看
四、非匹配

在正则表达式中,我们往往需要在字符串中进行非匹配,这时,就要通过^进行匹配条件限制,^的常见入门用法如下:

[^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范围中一个字符

代码示例如下:

public class RegExp {
private Pattern patt;
private Matcher matcher;

  public boolean squareReg(String regStr,String regex){
return this.commonRegExp(regStr, regex);
}

  private boolean commonRegExp(String regStr,String regex){
boolean wildcard_Res=false;
patt=Pattern.compile(regex);
matcher=patt.matcher(regStr);
wildcard_Res= matcher.find();
return wildcard_Res;
}
}

public class TestRegExp {
public static void main(String[] args) {
RegExp re=new RegExp();
boolean wildcard_Res=false;

     wildcard_Res=re.squareReg("tcn", "t[^aoe]n");
     System.out.println(wildcard_Res);
//输出:wildcard_Res=true
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: