java学习之路----java类库---正则表达式

1.现在有一个要求就是判断一段字符串是否由字符串组成?

               有两种方法:

     1、
public   class  RegexDemo {
     
       public   static   void  main(String[] args) {
          String str=  "1233333333333"  ;
          
            boolean  flag= true  ;
          
            char  a[]=str.toCharArray();
          
            for  ( int  i=0;i<a.  length ;i++){
                if  (a[i]< '0'  ||a[i]>  '9' ){
                   
                   flag=  false  ;
                     break  ;
              }
          }
          
            if  (flag){
              System.  out  .println( "是有数字组成"  );
          }  else  {
              System.  out  .println( "不是有数字组成"  );
          }
     }

}

结果:
是有数字组成

2.
      public   class  RegexDemo1 {
       public   static   void  main(String[] args) {
          
          String str=  "12333333333"  ;
            if  (Pattern.compile(  "[0-9]+"  ).matcher(str).matches()){
              System.  out  .println( "是有数字组成"  );
          }  else  {
               System.  out .println(  "不是有数字组成"  );
          }
          
     }

}

结果:
是有数字组成

通过上面的两段代码,显然第二段要少很多代码,这就是正则表达式


2.如果想要学会正则表达式你就必须知道Pattern类和Matcher类

      pattern类的作用是用来进行正则规范的,而Matcher类是用来执行规范的













3.下面来做一个简单的验证

                日期的要要求是:yyyy-MM-dd

                      正则表达式:
                                        例:2008-12-21
                                             \d{4}-\d{2}-\d{2}




public   class  RegexDemo2 {
            public   static  void  main(String[] args) {
              String str=  "2009-12-23"  ;
              
              String pat=  "\\d{4}-\\d{2}-\\d{2}"  ;
              
              Pattern pattern=Pattern. compile(pat);  //实例化Pattern类
              
              Matcher matcher=pattern.matcher(str);
              
                if  (matcher.matches()){
                   System.  out  .println( "日期格式合法"  );
              }  else  {
                   System.  out  .println( "日期格式不合法"  );
              }
              
          }

}

结果:
日期格式合法

4.进行字符串的拆分

           public   class  RegexDemo3 {
            public   static  void  main(String[] args) {
              
              String str=  "A12313B2312C131231D3432E"  ;
              String pat=  "\\d+"  ;
              
              Pattern pattern=Pattern. compile(pat);
              
              String s[]=pattern.split(str);
              
                for  ( int  i=0;i<s.  length ;i++){
                   System.  out  .print(s[i]+ "\t"  );
              }
              
          }

}

结果:

A    B    C    D    E


5.替换操作

           public   class  RegexDemo4 {
            public   static  void  main(String[] args) {
              String str=  "A21312B23123B2312C321D231231E"  ;
              
              String pat=  "\\d+"  ;
              
              Pattern pattern=Pattern. compile(pat);
              
              Matcher matcher=pattern.matcher(str);
              
              String newString=matcher.replaceAll(  "-"  );
              System.  out  .println(newString);
          }
}

结果:

A-B-B-C-D-E

5.String类对正则表达式的支持


      public   class  RegexDemo5 {
            public   static  void  main(String[] args) {
              String str= "A21312B31231C2321D3213E"  .replaceAll(  "\\d+" "-"  );
              
                boolean  temp= "2013-11-23"  .matches(  "\\d{4}-\\d{2}-\\d{2}" );
              
              String s[]= "A6SADAS67789ADS7D89AS"  .split( "\\d+"  );
              
              System.  out  .println(str);
              
              System.  out  .println(temp);
              
                for  ( int  i=0;i<s.  length ;i++){
                   System.  out  .print(s[i]+ "\t"  );
              }
              
          }

}

结果:

A-B-C-D-E
true
A    SADAS     ADS  D    AS   




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值