java正则表达式非匹配

如题所述

第1个回答  2022-11-12

  四 非匹配

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

  [^a z]     条件限制在非小写a to z范围中一个字符

  [^A Z]     条件限制在非大写A to Z范围中一个字符

  [^a zA Z] 条件限制在非小写a to z或大写A to Z范围中一个字符

  [^ ]     条件限制在非 to 范围中一个字符

  [^ a z] 条件限制在非 to 或a to z范围中一个字符

  代码示例如下:

   public class RegExp {

        private Pattern patt;

        private Matcher matcher;

  

      public boolean squareReg(String regStr String regex){

            return monRegExp(regStr regex);

        }

  

      private boolean monRegExp(String regStr String regex){

            boolean wildcard_Res=false;

            patt=pile(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

lishixinzhi/Article/program/Java/hx/201311/25692

相似回答
大家正在搜