Android Regular Expression _ regular expression

Source: Internet
Author: User
Tags gettext html tags

To strictly verify the mobile phone number, we must first know which numbers are open to the beginning of the number section, the current domestic number is divided into the following:

Mobile: 134, 135, 136, 137, 138, 139, 150, 151, 157 (TD), 158, 159, 187, 188

Unicom: 130, 131, 132, 152, 155, 156, 185, 186

Telecommunications: 133, 153, 180, 189, (1349 health)

Verify mobile phone Number:

public class Classpathresource {public 
  static Boolean Ismobileno (String mobiles) {pattern 
    p = Pattern 
        . Comp Ile ("^" ([-]) | ( [^,//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 ("")); 
  } 
}
public class Classpathresource {public  
  static Boolean Ismobileno (String mobiles) {pattern  
    p = pattern  
        . C Ompile ("^" ([-]) | ( [^,//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 ("") ;  
  }  

Verify mailbox:

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 ();  
}

Check to see if the rules are entered in the 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) ");  
}} 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) ");  }  
}

Common Regular Expression Collection

Regular expressions for string processing, form verification, and other occasions, practical and efficient. Some commonly used expressions are collected here to prepare for a rainy-future.

Matching regular expressions for Chinese characters: [\U4E00-\U9FA5]

Commentary: Matching Chinese is really a headache, with this expression will be easy to do

Match Double-byte characters (including Chinese characters): [^\x00-\xff]

Commentary: can be used to compute the length of a string (a double-byte character length meter 2,ascii 1 characters)

A regular expression that matches a blank row: \n\s*\r

Commentary: can be used to delete blank lines

Regular expression:< matching HTML tags (\s*?) [^>]*>.*?| <.*? />

Commentary: The online version is too bad, the above can only match the part of the complex nested tags still powerless

A regular expression that matches the end-end whitespace character: ^\s*|\s*

Commentary: A useful expression that can be used to delete white-space characters (including spaces, tabs, page breaks, and so on) at the end of a line at the beginning

Regular expression matching an email address: \w+ ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) *

Commentary: Form validation is useful

Regular expressions that match URL URLs: [a-za-z]+://[^\s]*

Commentary: Online circulation of the version of the function is very limited, which can meet the basic requirements

Match account number is legal (start of letter, allow 5-16 bytes, allow alphanumeric underline): ^[a-za-z][a-za-z0-9_]{4,15}

Commentary: Form validation is useful

Match domestic phone number: \d{3}-\d{8}|\d{4}-\d{7}

Commentary: Match form such as 0511-4405222 or 021-87888822

Matching Tencent QQ Number: [1-9][0-9]{4,}

Commentary: Tencent QQ number starting from 10000

Match China ZIP Code: [1-9]\d{5} (?! \d)

Commentary: China postal code is 6 digits

Matching ID: \d{15}|\d{18}

Commentary: China's ID card is 15-or 18-digit

Matching IP address: \d+\.\d+\.\d+\.\d+

Commentary: Useful when extracting IP addresses

Match a specific number:

^[1-9]\d*//Matching positive integer
^-[1-9]\d*//matching negative integers
^-? [1-9]\d*//matching integer
^[1-9]\d*|0//matching nonnegative integer (positive integer + 0)
^-[1-9]\d*|0//matching non positive integer (negative integer + 0)
^[1-9]\d*\.\d*|0\.\d*[1-9]\d*//matching positive floating-point numbers
^-([1-9]\d*\.\d*|0\.\d*[1-9]\d*)//matching negative floating-point number
^-? ([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0)//matching floating-point numbers
^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0//matching nonnegative floating-point number (positive floating-point number + 0)
^ (-([1-9]\d*\.\d*|0\.\d*[1-9]\d*)) |0?\.0+|0//matching non-positive floating-point numbers (negative floating-point number + 0)

Commentary: useful when dealing with large amounts of data, pay attention to corrections when applied

Match a specific string:

^[a-za-z]+//Match a string of 26 English letters
^[a-z]+//Match a string of 26 uppercase letters
^[a-z]+//Match string consisting of 26 lowercase letters
^[a-za-z0-9]+//Match a string of numbers and 26 English letters
^\w+//Match A string of numbers, 26 English letters, or underscores

Commentary: Some of the most basic and commonly used expressions

The above content is small to share with you the Android regular expression of the encyclopedia, hope for everyone useful.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.