A simple example of using regular expressions in Java and common regular sharing _java

Source: Internet
Author: User
Tags lowercase
 import Java.util.Scanner; public class Regextest {//new class public static void Main (string[] args) {//Main method Scanner sc = new Scanner (Sy stem.in); 
    New Scanner Class object System.out.println ("Please Enter Email:"); 
    String email = sc.nextline (); 
    System.out.println ("Please Enter Mobile:"); 
    String mobile = Sc.nextline ();  
    String email_regex= "\\w+@\\w+\\.\\w{2,3}"; String mobile_regex= "^ ((13[0-9)) | ( 15[0-9]) | (18[0-9]) \\D{8} "//Set mobile number regular expression Rule 13*,15*,18* if (Email.matches (Email_regex)) {System.out.println (email +) is a legal E 
    Mail address! ");} 
     else{System.out.println (email + "is an illegal email address!"); 
    } if (Mobile.matches (Mobile_regex)) {System.out.println (mobile + "is a legitimate mobile phone number"); 
    else{System.out.println (Mobile + "is an illegal mobile phone number"); } 
   } 
 } 

In the process of development, it is inevitable to encounter the need to match, find, replace, judge the situation of strings, and these situations are sometimes more complex, if the use of pure coding to solve, will often waste the programmer's time and energy. Therefore, learning and using regular expressions are the main means to solve this contradiction. As we all know, regular expressions are a specification that can be used for pattern matching and substitution. A regular expression is a literal pattern consisting of ordinary characters (such as characters A through Z) and special characters (metacharacters) that describe one or more strings to be matched when finding the body of the text. A regular expression is used as a template to match a character pattern with the string being searched for.

Since jdk1.4 launched the Java.util.regex package, it provides us with a good Java regular Expression application platform.

Common Regular Expression rules

Match specific number: ^[1-9]d*$//Match positive integer ^-[1-9]d*$//Match negative integer ^-? 
[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 number ^-([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 ^[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$//matching non-positive floating-point numbers (negative floating-point number + 0) Commentary: useful when dealing with a large amount of data, the specific use of attention to modify matching specific strings: ^[a-za-z]+$//Match a string of 26 English letters ^[a-z]+$//Match by 26 English letters in uppercase The composed string ^[a-z]+$//matches a string consisting of 26 lowercase letters ^[a-za-z0-9]+$//matches a string of numbers and 26 English letters ^w+$//matching a string of digits, 26 letters, or underscores using Reg 
Ularexpressionvalidator validation of a control and its validation expression is described as follows: You can only enter a number: "^[0-9]*$" can only enter n digits: "^d{n}$" can only enter at least n digits: "^d{n,}$" Only m-n digits can be entered: "^d{m,n}$" can only enter numbers beginning with 0 and not 0: "^ (0|[ 1-9][0-9]* $ "Only positive real number with two decimal digits:" ^[0-9]+ (. [ 0-9]{2})? $ "only positive real numbers with 1-3 decimal digits are entered:" ^[0-9]+ (. [ 0-9]{1,3})? $ "Only a Non-zero positive integer can be entered:" ^+? [1-9] [0-9]*$] can only enter a Non-zero negative integer: "^-[1-9][0-9]*$" can only enter characters with a length of 3: "^. {3}$ "can only enter a string of 26 English letters:" ^[a-za-z]+$ "can only enter a string of 26 uppercase letters:" ^[a-z]+$ "can only be entered by 26 lowercase English character string: "^[a-z]+$" can only enter a string of numbers and 26 English letters: "^[a-za-z0-9]+$" can only enter a string consisting of numbers, 26 letters, or underscores: "^w+$" verifies the user password: "^[ 
a-za-z]w{5,17}$ "The correct format is: Start with a letter, length between 6-18, can only contain characters, numbers and underscores." Verify that there are ^%& ',; =?$ ' characters such as: "[^%& ',; =? $x 22]+" Can only enter Chinese characters: "^[u4e00-u9fa5],{0,}$" Verify email Address: "^w+[-+.] w+) *@w+ ([-.] w+) *.w+ ([-.] w+) *$ "Verify InternetURL:" ^http://([w-]+.) +[w-]+ (/[w-./?%&=]*)? $ "Verify phone Number:" ^ (d{3,4}) |d{3,4}-)? d{7,8}$ "The correct format is:" Xxxx-xxxxxxx "," xxxx-xxxxxxxx "," " 
Xxx-xxxxxxx "," xxx-xxxxxxxx "," XXXXXXX "," XXXXXXXX ". Verify ID Number (15-bit or 18-digit): "^d{15}|d{}18$" verification 12 months of the year: "^ (0?[ 1-9]|1[0-2]) $ "The correct format is:" 01 "-" 09 "and" 1 "" 12 "to verify one days of months:" ^ (0?[ 1-9]) | 
 
((1|2) [0-9]) |30|31) $ "The correct format is:" 01 "" 09 "and" 1 "" 31 ". A regular expression that matches a Chinese character: [U4e00-u9fa5] matches double-byte characters (including Chinese characters): [^x00-xff] A regular expression that matches a blank row: n[s|] *r regular expression that matches the HTML tag:/< (. *) >.*|< (. *)/>/matches a regular expression with a trailing space: (^s*) | (s*$) A regular expression matching an email address: w+ ([-+.] w+) *@w+ ([-.] w+) *.w+ ([-.] w+) * Matching the URL of the regular expression: http://([w-]+.) 
 +[w-]+ (/[w-/?%&=]*)?

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.