zoukankan      html  css  js  c++  java
  • java正则表达式(四)

    四、非匹配

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

    [^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 }

     

     

  • 相关阅读:
    Notes of Daily Scrum Meeting(12.22)
    一个合格的程序员应该读过哪些书
    snprintf vs sprintf
    Centos 关闭图形界面
    oracle selinux 问题
    struct 和typedef struct的区别
    c语言字符串函数
    504. Base 7
    汉诺塔python实现
    VIM字符编码基础知识
  • 原文地址:https://www.cnblogs.com/loritin/p/2725024.html
Copyright © 2011-2022 走看看