(轉)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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章