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

  • 2020-04-01 03:54:11
  • OfStack

import java.util.Scanner;  
 
public class regexTest {  //The new class
  public static void main(String[] args){  //The main method
     
    Scanner sc = new Scanner(System.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 phone number regular expression rules 13*,15*,18*
      
    if(email.matches(Email_regex)){ 
       System.out.println(email + " It's legal Email Address!"); 
    } 
    else{ 
       System.out.println(email + " It's illegal Email Address!"); 
     } 
     
    if(mobile.matches(Mobile_regex)){ 
     System.out.println(mobile + " It's a legitimate cell phone number "); 
    } 
    else{ 
      System.out.println(mobile + " It's an illegal cell phone number "); 
    } 
   } 
 } 

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

Since the release of the java.util.regex package in jdk1.4, it has provided us with a good platform for Java regular expression applications.

Common regular expression rules


 Match specific Numbers:  
^[1-9]d*$    //Matching positive integer
^-[1-9]d*$   //Matching negative integer
^-?[1-9]d*$   //Match the integer
^[1-9]d*|0$  //Match a nonnegative integer (positive integer + 0)
^-[1-9]d*|0$   //Match a nonpositive integer (negative integer + 0)
^[1-9]d*.d*|0.d*[1-9]d*$   //Matches a positive floating point number
^-([1-9]d*.d*|0.d*[1-9]d*)$  //Matches a negative floating point number
^-?([1-9]d*.d*|0.d*[1-9]d*|0?.0+|0)$  //Matched floating point number
^[1-9]d*.d*|0.d*[1-9]d*|0?.0+|0$   //Match non-negative floating point (positive floating point + 0)
^(-([1-9]d*.d*|0.d*[1-9]d*))|0?.0+|0$  //Match non-positive floating point (negative floating point + 0)
 Note: useful when dealing with a large amount of data, pay attention to correction when specific application  
 
 Match a specific string:  
^[A-Za-z]+$  //Matches a string of 26 English letters
^[A-Z]+$  //Matches a string of 26 uppercase English letters
^[a-z]+$  //Matches a string of 26 English letters in lowercase
^[A-Za-z0-9]+$  //Matches a string of Numbers and 26 English letters
^w+$  //Matches a string of Numbers, 26 English letters, or underscores
 
 In the use of RegularExpressionValidator The validation function and its validation expression when validating the control are described below : 
 
 Enter only Numbers:" ^[0-9]*$ "  
 Can only enter n Bit number:" ^d{n}$ "  
 You can only type in at least n Digit number:" ^d{n,}$ "  
 Can only enter m-n Bit number:" ^d{m,n}$ "  
 Enter only Numbers starting with zero and non-zero:" ^(0|[1-9][0-9]*)$ "  
 Only positive real Numbers with two decimal places can be entered:" ^[0-9]+(.[0-9]{2})?$ "  
 You can only type in 1-3 Positive real number of decimal places:" ^[0-9]+(.[0-9]{1,3})?$ "  
 Only positive non-zero integers can be entered:" ^+?[1-9][0-9]*$ "  
 Can only enter non-zero negative integers:" ^-[1-9][0-9]*$ "  
 You can only enter length is 3 Of:" ^.{3}$ "  
 Can only be entered by 26 A string of English letters:" ^[A-Za-z]+$ "  
 Can only be entered by 26 A string of capitalized English letters:" ^[A-Z]+$ "  
 Can only be entered by 26 A string of lowercase English letters:" ^[a-z]+$ "  
 Can only be input by Numbers and 26 A string of English letters:" ^[A-Za-z0-9]+$ "  
 Can only be input by Numbers, 26 A string of English letters or underscores:" ^w+$ "  
 Verify user password : " ^[a-zA-Z]w{5,17}$ "The correct format is: start with a letter, the length in 6-18 Between,  
 
 Only characters, Numbers, and underscores can be included.  
 Verify the presence of ^%&',;=?$ Characters such as" [^%&',;=?$x22]+ "  
 Can only input Chinese characters:" ^[u4e00-u9fa5],{0,}$ "  
 validation Email Address:" ^w+[-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*$ "  
 validation 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 or 18 Digit number) :" ^d{15}|d{}18$ "  
 Verified for one year 12 Months," ^(0?[1-9]|1[0-2])$ The correct format is: 01 " - " 09 "And" 1 "" 12 "  
 Verify one month's worth 31 Day:" ^((0?[1-9])|((1|2)[0-9])|30|31)$ "  
 
 The correct format is:" 01 "" 09 "And" 1 "" 31 ".  
 
 Regular expressions matching Chinese characters:  [u4e00-u9fa5] 
 Matches double-byte characters ( Including Chinese characters ) : [^x00-xff] 
 Regular expression matching empty lines: n[s| ]*r 
 matching HTML Marked regular expressions: /< (.*)>.*|< (.*) />/ 
 Regular expressions matching Spaces: (^s*)|(s*$) 
 matching Email Regular expression of address: w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)* 
 Match the url URL Regular expression of: http://([w-]+.)+[w-]+(/[w- ./?%&=]*)? 


Related articles: