国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > regex > 正文

Android正則表達式

2020-01-20 22:09:39
字體:
來源:轉載
供稿:網友

要嚴格的驗證手機號碼,必須先要清楚現在已經開放了哪些數字開頭的號碼段,目前國內號碼段分配如下:

移動:134、135、136、137、138、139、150、151、157(TD)、158、159、187、188

聯通:130、131、132、152、155、156、185、186

電信:133、153、180、189、(1349衛通)

驗證手機號:

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中輸入的是否符合規則:

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("http://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("http://w*[.](Java|cpp|class)");    }  } 

常用正則表達式收集

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

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

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

匹配雙字節字符(包括漢字在內):[^/x00-/xff]

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

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

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

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

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

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

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

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

評注:表單驗證時很實用

匹配網址URL的正則表達式:[a-zA-z]+://[^/s]*

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

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

評注:表單驗證時很實用

匹配國內電話號碼:/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位數字

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

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

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

評注:提取ip地址時有用

匹配特定數字:

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

評注:處理大量數據時有用,具體應用時注意修正

匹配特定字符串:

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

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

以上內容就是小編跟大家分享的android正則表達式大全,希望對大家有用。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 斗六市| 门源| 会同县| 平凉市| 延庆县| 苍南县| 团风县| 中宁县| 札达县| 萨迦县| 察隅县| 荣成市| 新宾| 扎兰屯市| 四平市| 日喀则市| 黄浦区| 张家口市| 洛阳市| 呈贡县| 宝鸡市| 葵青区| 时尚| 政和县| 临沧市| 伊宁县| 峨眉山市| 明星| 临潭县| 云浮市| 钦州市| 苍南县| 东丽区| 新津县| 莱州市| 洪湖市| 南宁市| 荆门市| 济阳县| 修武县| 伊通|