Regular expressions in Java

Posted by jmakeig on Fri, 04 Feb 2022 10:22:17 +0100

Regular expression:

The use of regular is convenient for verification applications

package Demo_2_4_regular expression ;

public class Main {
    public static void main(String[] args) {
        String str = "1234";
        if (str.matches("\\d+")){
            int num = Integer.parseInt(str);
            System.out.println(num * 2);
        }
    }
}

1. [quantity: single] character matching

- any character: indicates that it is composed of any character.

public class Main {
    public static void main(String[] args) {
        String str = "a";   // Data to judge
        String regex = "a";      // Regular expressions can only match a single a ,Cannot match other characters or multiple characters a 
        System.out.println(str.matches(regex));
    }
}

- \ \: match a '\'.

- \ n: match wrap

- \ t: match table character

2. [quantity: single] character set matching

- [abc]: indicates any one of the letters a, b and c

public class Main {
    public static void main(String[] args) {
        String str = "b";   // Data to judge
        String regex = "[abc]";      // regular expression 
        System.out.println(str.matches(regex));
    }
}

- [^ abc]: indicates that it is not any letter of a, b or c

- [a-zA-Z]: indicates that it is composed of any letter and is not case sensitive

public class Main {
    public static void main(String[] args) {
        String str = "a";   // Data to judge
        String regex = "[a-zA-Z]";      // regular expression 
        System.out.println(str.matches(regex));
    }
}

- [0-9]: refers to any number between 0 and 9

public class Main {
    public static void main(String[] args) {
        String str = "1";   // Data to judge
        String regex = "[0-9]";      // regular expression 
        System.out.println(str.matches(regex));
    }
}

3. [quantity: single] simplified character set

  - " . " : Represents any character

public class Main {
    public static void main(String[] args) {
        String str = "#";   // Data to judge
        String regex = ".";      // regular expression 
        System.out.println(str.matches(regex));
    }
}

- \ d: equivalent to [0-9]

- \ D: equivalent to [^ 0-9]

- \ s: match any space, which may be space, line feed or tab;

public class Main {
    public static void main(String[] args) {
        String str = "a\n";   // Data to judge
        String regex = "\\D\\s";      // regular expression 
        System.out.println(str.matches(regex));
    }
}

- \ S: match any non whitespace data

- \ w: match letters, numbers and underscores, equivalent to [a-zA-Z_0-9]

- \ W: match non letters, numbers and underscores, equivalent to [^ a-zA-Z_0-9]

4. Boundary matching:

- ^: start of matching boundary

- $: end of matching boundary

5. Quantity expression: by default, multiple characters can be matched only when the quantity unit is added

- "expression?": Indicates that the regular expression can appear 0 or 1 times

- "expression *": indicates that the regular expression can appear 0 or more times

- "expression +": indicates that the regular expression can appear one or more times

public class Main {
    public static void main(String[] args) {
        String str = "aabc";   // Data to judge
        String regex = "\\w+";      // regular expression 
        System.out.println(str.matches(regex));
    }
}

- expression {n}: the length of the regular expression is exactly n times

- expression {n,}: the length of regular expression is more than n times

- expression {n.m}: the length of regular expression is n~ m times

package Demo_2_4_regular expression ;

public class Main {
    public static void main(String[] args) {
        String str = "aabc";   // Data to judge
        String regex = "\\w{3,}";      // regular expression ,\\w express\w:Match non letters, numbers, underscores, equivalent to[^a-zA-Z_0-9],Match 3 times or more
        System.out.println(str.matches(regex));
    }
}

6. Logical expression: multiple regular expressions can be connected

- expression X expression Y: the X expression is followed by the Y expression

public class Main {
    public static void main(String[] args) {
        String str = "aabc";   // Data to judge
        String regex = "ax";      // Regular expressions, a We must keep up x
        System.out.println(str.matches(regex));
    }
}

- expression X | expression Y: one expression can be satisfied

- (expression): an overall description of the expression. The quantity unit can be set for the overall description

 

Use regular matching:

Remove non alphanumeric symbols from a string:

public class Main {
    public static void main(String[] args) {
        String str = "ax12@#qwetquyw12@$#!()qpdoihj2oi23*#(*!498yqHR98@!#¥(*98HR#*(hr3F*(hF#Q$*(HFH''";   // Data to judge
        String regex = "[^a-zA-Z0-9]+";      // regular expression 
        System.out.println(str.replaceAll(regex,""));
    }
}

Split strings alphabetically:

public class Main {
    public static void main(String[] args) {
        String str = "a1111b2222c333d4444e7777";   // Data to judge
        String regex = "\\d+";      // regular expression 
        System.out.println(str.replaceAll(regex,""));
        for (String s : str.split(regex)) {
            System.out.println(s);
        }
    }
}

Judge whether it is decimal:

public class Main {
    public static void main(String[] args) {
        String str = "100.223";   // Data to judge
        String regex = "\\d+\\.\\d+";      // regular expression 
        System.out.println(str.matches(regex));
    }
}

However, this method has defects. When it is judged as an integer or only a decimal point, the result is false,Therefore, it is improved to the following code:

 

public class Main {
    public static void main(String[] args) {
        String str = "100";   // Data to judge
        String regex = "\\d+(\\.\\d+)?";      // regular expression 
        System.out.println(str.matches(regex));
    }
}

 

Judge date type string:

import java.text.SimpleDateFormat;

public class Main {
    public static void main(String[] args) throws Exception{
        String str = "2020-12-11";   // Data to judge
        String regex = "\\d{4}-\\d{2}-\\d{2}";      // regular expression 
        if (str.matches(regex)){    // Conform to regular expression
            System.out.println(new SimpleDateFormat("yyy-MM-dd").parse(str));
        }
    }
}

The above codes can only be judged according to the digital format, and the content cannot be judged.

Determine whether the given phone number is correct?
Telephone number:51283346,\\d{7,8}; 
Telephone number:01051283346,(\\d{3,4})?\\d{7,8}; 
Telephone number:(010)-51283346,((\\d{3,4})(\\(\\d{3,4}\\)-))?\\d{7,8}. 

Matching mailbox:

package Demo_2_4_regular expression ;

import java.text.SimpleDateFormat;

public class Main {
    public static void main(String[] args) throws Exception{
        String str = "cdulmjava@cdu.edu.cn";   // Data to judge
        String regex = "[a-zA-Z0-9]*.[a-zA-Z]*.[a-zA-Z]*.([a-zA-Z]*)?";      // regular expression 
        String regex1 = "[a-zA-Z0-9]\\w+@[a-zA-Z]+.(cn|com|com.cn|net|gov)+";      // regular expression 
        System.out.println(str.matches(regex1));
    }
}

 

Topics: Java