Home>

To strictly verify the phone number,It ’s important to know which numbers start with numbers.The current domestic number band assignments are as follows:

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 Satcom)

Verify phone number:

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

Verify email:

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 if the text entered in edittext complies with the rules:

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

Collection of common regular expressions

Regular expressions are used in string processing, form validation, etc.Practical and efficient.Collect some common expressions here,in case for need.

Regular expression matching Chinese characters:[\ u4e00- \ u9fa5]

Commentary:Matching Chinese is really a headache.With this expression is easy to handle

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

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

Regular expression matching blank lines:\ n \ s * \ r

Commentary:Can be used to delete blank lines

Regular expressions matching html tags:<(\ s *?) [^>] *&.;**?|<. *?/>

Commentary:The version circulating online is too bad,The above one can only match part,Powerless for complex nested tags

Regular expression matching leading and trailing whitespace characters:^ \ s * | \ s *

Comment:Can be used to remove whitespace at the beginning and end of a line (including spaces, tabs, form feeds, etc.), very useful expressions

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

Commentary:Useful for form validation

Regular expression matching URL url:[a-za-z] +://[^ \ s] *

Commentary:The version circulated online is very limited.The above can basically meet the needs

Match account is legal (beginning with a letter,5-16 bytes allowed, alphanumeric underscores allowed):^ [a-za-z] [a-za-z0-9 _] {4,15}

Commentary:Useful for form validation

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

Commentary:Matches such as 0511-4405222 or 021-87888822

Match Tencent QQ number:[1-9] [0-9] {4,}

Commentary:Tencent qq number starts from 10000

Match Chinese postal code:[1-9] \ d {5} (?! \ D)

Commentary:China postcode is 6 digits

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

Commentary:Chinese ID is 15 or 18

Match IP address:\ d + \. \ D + \. \ D + \. \ D +

Commentary:Useful when extracting IP addresses

Match specific numbers:

^ [1-9] \ d * //Match a positive integer

^-[1-9] \ d * //Match a negative integer

^-?[1-9] \ d * //Match integer

^ [1-9] \ d * | 0 //Match non-negative integer (positive integer + 0)

^-[1-9] \ d * | 0 //Match a non-positive integer (negative integer + 0)

^ [1-9] \ d * \. \ D * | 0 \. \ D * [1-9] \ d * //Match a positive floating point number

^-([1-9] \ d * \. \ D * | 0 \. \ D * [1-9] \ d *) //Match a negative floating point number

^-?([1-9] \ d * \. \ D * | 0 \. \ D * [1-9] \ d * | 0?\. 0+ | 0)/match Floating point number

^ [1-9] \ d * \. \ D * | 0 \. \ D * [1-9] \ d * | 0?\. 0+ | 0 //match non-negative floating point Points + 0)

^ (-([1-9] \ d * \. \ D * | 0 \. \ D * [1-9] \ d *)) | 0?\. 0+ | 0 //Match non-positive Floating point number (negative floating point number + 0)

Commentary:useful when dealing with large amounts of data,Pay attention to correction during specific applications

Matches a specific string:

^ [a-za-z] + //matches a string of 26 English letters

^ [a-z] + //matches a string of 26 English capitals

^ [a-z] + //Matches a string consisting of 26 English lowercase letters

^ [a-za-z0-9] + //matches a string of numbers and 26 English letters

^ \ w + //Matches a string of numbers, 26 English letters, or underscores

Commentary:The most basic and most commonly used expressions

The above is the android regular expressions we share with you,hope its good for U.S..

  • Previous Regular expressions of mobile phone number and license plate number in Android
  • Next Detailed date and time class and Calendar class usage in Java