要嚴格的驗證手機號碼,必須先要清楚現在已經開放了哪些數字開頭的號碼段,目前國內號碼段分配如下:
移動: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("^((13[0-9])|(15[^4,//D])|(18[0,5-9]))//d{8}$");
- 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("18977778989"));
- }
- }
驗證郵箱:
- 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("\\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地址時有用
匹配特定數字:
^[0-9]*[1-9][0-9]*$ //匹配正整數
^-[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個英文字母或者下劃線組成的字符串
評註:最基本也是最常用的一些表達式