(转)Java中关于空格替换的正则表达式,实例代码

句点符号 . 是通配符 ,  * 零次或多次,+ 一次或多次,? 零次或一次,{n} 恰好n次,{n,m} 从n到m次
要记住,老是拿*当通配符,实际是 句号 . 是通配符

 

 

package test.function.excel;

public class RegTest {
 
 public static void main(String[] args) {
   
  //Java中关于空格的正则表达式
  
  
    String str2 = "GET             /index.html HTTP/1.1";  //字符串s由“GET”、“/index.html”和“HTTP/1.1”组成,中间有一个或多个空格
    String tt[] = str2.split("\\s{1,}");   //按照空格分割字符串,多个空格作为一个空格对字符串进行分割
    for(String str: tt)//增强的for循环
    System.out.println(str);//输出:GET
                                       //  /index.html
                                      //  HTTP/1.1         
   
    String qq = str2.replaceAll(" {2,}", " ");//把字符串s中的多个空格替换为一个空格
    System.out.println(qq);//输出:GET /index.html HTTP/1.1
    System.out.println(str2);//输出:GET             /index.html HTTP/1.1
   
   
    // //split 按照空格分割字符串,多个空格作为一个空格对字符串进行分割
    String strTest = "668947   18  109451074 0                0        33       700198   2335821 " ;
    String resSplit[] = strTest.split("\\s{1,}")  ;
    for(int j =0 ;j< resSplit.length;j++){
     System.out.println(resSplit[j]);
    }
   
   
    //句点符号 . 是通配符 ,  * 零次或多次,+ 一次或多次,? 零次或一次,{n} 恰好n次,{n,m} 从n到m次
    String time = "dfda11:50:39" ;
    if(time.matches(".*\\d{2}:\\d{2}:\\d{2}")){
     System.out.println("fu he");
       }else{
       System.out.println("bu fu he");
       }
   
    // 不使用句点
    String time2 = "dfda11:50:39" ;
    if(time2.matches("\\d{2}:\\d{2}:\\d{2}")){
     System.out.println("fu he");
       }else{
       System.out.println("bu fu he");
       }
   


 }  

}

 

转自:http://www.51testing.com/?uid-202848-action-viewspace-itemid-226265

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值