Android中如何使用正則表達式

Android中如何使用正則表達式,針對這個問題,這篇文章詳細介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

新榮ssl適用于網(wǎng)站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為創(chuàng)新互聯(lián)公司的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:028-86922220(備注:SSL證書合作)期待與您的合作!

驗證手機號:

public class ClassPathResource { 
  public static boolean isMobileNO(String mobiles) { 
    Pattern p = Pattern 
        .compile("^(([-])|([^,//D])|([,-]))//d{}$"); 
    Matcher m = p.matcher(mobiles); 
    System.out.println(m.matches() + "---"); 
    return m.matches(); 
  } 
  public static void main(String[] args) throws IOException { 
    System.out.println(ClassPathResource.isMobileNO("")); 
  } 
}
public class ClassPathResource {  
  public static boolean isMobileNO(String mobiles) {  
    Pattern p = Pattern  
        .compile("^(([-])|([^,//D])|([,-]))//d{}$");  
    Matcher m = p.matcher(mobiles);  
    System.out.println(m.matches() + "---");  
    return m.matches();  
  }  
  public static void main(String[] args) throws IOException {  
    System.out.println(ClassPathResource.isMobileNO(""));  
  }  
}

驗證郵箱:

public static boolean isEmail(String strEmail) {  
  String strPattern = "^[a-zA-Z][\\w\\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]\\.[a-zA-Z][a-zA-Z\\.]*[a-zA-Z]$"; 
  Pattern p = Pattern.compile(strPattern); 
  Matcher m = p.matcher(strEmail); 
  return m.matches(); 
} 
public static boolean isEmail(String strEmail) {  
  String strPattern = "^[a-zA-Z][\\w\\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]\\.[a-zA-Z][a-zA-Z\\.]*[a-zA-Z]$";  
  Pattern p = Pattern.compile(strPattern);  
  Matcher m = p.matcher(strEmail);  
  return m.matches();  
}

檢查EditText中輸入的是否符合規(guī)則:

import Android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button;
import android.widget.EditText;  
public class Main extends Activity { 
  private EditText editText; 
  private Button button; 
  
  @Override 
  public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    editText = (EditText) findViewById(R.id.textId); 
    editText.setText("EditText element"); 
    button = (Button) findViewById(R.id.btnId); 
    button.setText("Check"); 
    button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
        if (checkString(editText.getText().toString())) { 
          editText.setText("Corect"); 
        } 
      } 
    }); 
  } 
  
  private boolean checkString(String s) { 
    return s.matches("\\w*[.](Java|cpp|class)"); 
  } 
}
import Android.app.Activity;  
import android.os.Bundle;  
import android.view.View;  
import android.widget.Button;  
import android.widget.EditText;  
public class Main extends Activity {  
  private EditText editText;  
  private Button button;  
  
  @Override 
  public void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.main);  
    editText = (EditText) findViewById(R.id.textId);  
    editText.setText("EditText element");  
    button = (Button) findViewById(R.id.btnId);  
    button.setText("Check");  
    button.setOnClickListener(new View.OnClickListener() {  
      @Override 
      public void onClick(View v) {  
        if (checkString(editText.getText().toString())) {  
          editText.setText("Corect");  
        }  
      }  
    });  
  }  
  
  private boolean checkString(String s) {  
    return s.matches("\\w*[.](Java|cpp|class)");  
  }  
}

常用正則表達式收集

正則表達式用于字符串處理、表單驗證等場合,實用高效?,F(xiàn)將一些常用的表達式收集于此,以備不時之需。

匹配中文字符的正則表達式: [\u4e00-\u9fa5]

評注:匹配中文還真是個頭疼的事,有了這個表達式就好辦了

匹配雙字節(jié)字符(包括漢字在內(nèi)):[^\x00-\xff]

評注:可以用來計算字符串的長度(一個雙字節(jié)字符長度計2,ASCII字符計1)

匹配空白行的正則表達式:\n\s*\r

評注:可以用來刪除空白行

匹配HTML標記的正則表達式:<(\S*?)[^>]*>.*?|<.*? />

評注:網(wǎng)上流傳的版本太糟糕,上面這個也僅僅能匹配部分,對于復雜的嵌套標記依舊無能為力

匹配首尾空白字符的正則表達式:^\s*|\s*

評注:可以用來刪除行首行尾的空白字符(包括空格、制表符、換頁符等等),非常有用的表達式

匹配Email地址的正則表達式:\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*

評注:表單驗證時很實用

匹配網(wǎng)址URL的正則表達式:[a-zA-z]+://[^\s]*

評注:網(wǎng)上流傳的版本功能很有限,上面這個基本可以滿足需求

匹配帳號是否合法(字母開頭,允許5-16字節(jié),允許字母數(shù)字下劃線):^[a-zA-Z][a-zA-Z0-9_]{4,15}

評注:表單驗證時很實用

匹配國內(nèi)電話號碼:\d{3}-\d{8}|\d{4}-\d{7}

評注:匹配形式如 0511-4405222 或 021-87888822

匹配騰訊QQ號:[1-9][0-9]{4,}

評注:騰訊QQ號從10000開始

匹配中國郵政編碼:[1-9]\d{5}(?!\d)

評注:中國郵政編碼為6位數(shù)字

匹配身份證:\d{15}|\d{18}

評注:中國的身份證為15位或18位

匹配ip地址:\d+\.\d+\.\d+\.\d+

評注:提取ip地址時有用

匹配特定數(shù)字:

^[1-9]\d*    //匹配正整數(shù)
^-[1-9]\d*   //匹配負整數(shù)
^-?[1-9]\d*   //匹配整數(shù)
^[1-9]\d*|0  //匹配非負整數(shù)(正整數(shù) + 0)
^-[1-9]\d*|0   //匹配非正整數(shù)(負整數(shù) + 0)
^[1-9]\d*\.\d*|0\.\d*[1-9]\d*   //匹配正浮點數(shù)
^-([1-9]\d*\.\d*|0\.\d*[1-9]\d*)  //匹配負浮點數(shù)
^-?([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0)  //匹配浮點數(shù)
^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0   //匹配非負浮點數(shù)(正浮點數(shù) + 0)
^(-([1-9]\d*\.\d*|0\.\d*[1-9]\d*))|0?\.0+|0  //匹配非正浮點數(shù)(負浮點數(shù) + 0)

評注:處理大量數(shù)據(jù)時有用,具體應(yīng)用時注意修正

匹配特定字符串:

^[A-Za-z]+  //匹配由26個英文字母組成的字符串
^[A-Z]+  //匹配由26個英文字母的大寫組成的字符串
^[a-z]+  //匹配由26個英文字母的小寫組成的字符串
^[A-Za-z0-9]+  //匹配由數(shù)字和26個英文字母組成的字符串
^\w+  //匹配由數(shù)字、26個英文字母或者下劃線組成的字符串

評注:最基本也是最常用的一些表達式

關(guān)于Android中如何使用正則表達式問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識。

分享文章:Android中如何使用正則表達式
文章地址:http://bm7419.com/article2/ijhcoc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、做網(wǎng)站、品牌網(wǎng)站設(shè)計、微信公眾號、品牌網(wǎng)站建設(shè)、小程序開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)