Java learning note 21 (String class supplement: regular expression)

Just like the re module of python, but there are some differences between Java and Python's regular expressions. For details, please refer to other articles on the Internet:

Function: it can be used to verify a string, such as verifying user name, password format, mailbox, etc

Example:

"[0-9] {6,Twelve}" matching rule: 6 to One 2 digits, for example, 123 Forty-five 6 Seventy-eight 9 is true, 12345 is false

"1 [3578] [0-9] {9}" matching rule: 1 at the beginning, the second digit is one of 3578, and the last nine arbitrary numbers, for example, 13345678912 is true

"a*b" matching rule: after multiple A's or 0 a's, there is a b, and b must be the last character. For example, aaaaab is true, abc is false

 

There are several methods in the String class that were not mentioned in the previous article, which will be used here

Basic knowledge will not be introduced any more, for example:

matches method:

package demo;

public class RegexDemo {
    public static void main(String[] args) {
        //inspect QQ Number, cannot start with 0, full number, 5 to 10 digits
        checkQQ();
        //Check the mobile phone number. The first digit is 1, and the second digit is 34578. There are 11 digits in total
        checkTel();
    }
    public static void checkQQ(){
        String QQ = "123456";
        boolean b = QQ.matches("[1-9][\\d]{4,9}");
        System.out.println(b);//true
    }
    public static void checkTel(){
        String telNumber = "13312345678";
        boolean b = telNumber.matches("1[34578][\\d]{9}");
        System.out.println(b);//true
    }
}

 

split method:

package demo;

public class RedexDemo {
    
    public static void main(String[] args) {
        split_1();
        split_2();
        split_3();
    }
    
    public static void split_1(){
        String str = "I   love         java";
        String[] strArr = str.split(" +");
        for (int i = 0; i < strArr.length; i++) {
            System.out.println(strArr[i]);
        }
        //Print out: I         love       java
    }
    
    public static void split_2(){
        String str = "10-12-45-78-Sixty-two";
        String[] strArr = str.split("-");
        for (int i = 0; i < strArr.length; i++) {
            System.out.println(strArr[i]);
        }
        //Printed out: 10           12        45        78        62
    }
    
    public static void split_3(){
        String str = "192.One hundred and sixty-eight.One hundred and sixty.1";
        String[] strArr = str.split("\\.");
        for (int i = 0; i < strArr.length; i++) {
            System.out.println(strArr[i]);
        }
        //Printed out: 192    168        160        1
    }
}

 

 

replaceAll method:

package demo;

public class RedexDemo {
    
    public static void main(String[] args) {
        replaceAll_1();
    }
    
    public static void replaceAll_1(){
        String str = "Hello666123Ilove789java";
        String str1 = str.replaceAll("[\\d+]","#");
        System.out.println(str1);
        //Output: Hello######Ilove###java
        
        String str2 = str.replaceAll("[\\d]+", "#");
        System.out.println(str2);
        //Output: Hello#Ilove#java
    }
}

 

 

Regular expression exercise:

 

package demo;

public class RedexDemo {
    public static void main(String[] args) {
        //Check email address
        checkMail();
    }
    public static void checkMail(){
        String email = "abc123@sina.com.cn";
        boolean b = email.matches("[a-zA-Z0-9_]+@[0-9a-z]+(\\.[a-z]+)+");
        System.out.println(b);//true
    }
}

Tags: Java Python Mobile

Posted on Mon, 04 May 2020 11:25:44 -0400 by Sirus121