Take you to master regular expression quickly

Keywords: Programming Java Mobile

Regular expression

1, Introduction & overview

  • Why regular expressions?

Help us simplify string operation (judgment, replacement, cutting ).

Regular expressions: expressions with correct rules

 

  • Effect:

Simplify string operation (judgment, replacement, cutting ).

 

  • Pseudo code:

Determine whether the current string is a QQ number

The correct formula: "it is required to be a pure number, 5-10 digits, and cannot start with 0"

 

 

 

2, Getting started with

If you don't say much, use it in the case directly:

Check whether the QQ number in the verification string is as follows:

① Must be 5-15 digits

② Cannot start with 0

If you don't use regular, it's like this:

/*
* Introduction case: not regular
* */
public static void main(String[] args) {
    //1. Prepare a QQ number to be verified
    String str = "1231234";
    //2. Judgment: QQ number must be a number
    try {
        int i = Integer.parseInt(str);
    } catch (NumberFormatException e) {
        System.out.println("QQ No. No.");
        return;
    }
    //3. Judgment: QQ No. between 5 and 15 bits cannot start with 0
    if(str.length()>=5 && str.length()<=15
            && !str.startsWith("0")){
        System.out.println("QQ The number format is correct");
    }else{
        System.out.println("QQ No. No.");
    }
}

 

 

Use regular:

/*
* Introduction case: using regular
* */
public static void main(String[] args) {
    //1. Prepare a QQ number to be verified
    String str = "1231234";
    //2. Defining a regular expression in Java programming at this stage, regular expressions are all in the form of strings
    String reg = "[1-9][0-9]{4,14}";
    //3. Judge matches
    boolean b = str.matches(reg);
    System.out.println(b?"qq The number format is correct":"qq No. No.");
}

 

 

 

 

 

 

3, Usage of regular expressions in Java [emphasis]

Method name

describe

boolean matches(String regex)

Whether the current string matches the given regular expression

true: matching

false: mismatch

String replaceAll(String regex,String replacement)

Replace the contents of all symbol regular expressions in the current string with replacement

String[] split(String regex)

Splits the current string based on the given regular expression

 

public static void main(String[] args) {
    //Requirement: verify mobile phone number
    //1. 11 digits
    //2. First digit is 1, second digit is 0-9 after 3,4,5,7,8
    String num = "18810033112";
    String reg = "1[34578][0-9]{9}";
    System.out.println(num.matches(reg));
}

 

public static void main(String[] args) {
//        Example: replace all the numbers in the string with! Number
        String str = "1a2b3c4d";
        //Regular:
        System.out.println(str.replaceAll("[0-9]","!"));
        System.out.println(str.replaceAll("\\d","!"));
    }

 

public static void main(String[] args) {
        String str = "abcAAAabcAAAcdeAAAccAA";
//        Replace all AAA with 0
//        Effect: abc0abc0cde0ccAA
        String reg = "[A]{3}";
        String ss = str.replaceAll(reg, "0");
        System.out.println(ss);
}

 

public static void main(String[] args) {
    String str = "You said that today's weather is not what you want, you want the whole world to be what you want to be. You told me that today you want to eat cake, I bought cake, but you said that today you want to lose weight";
    System.out.println(str.replaceAll("you.","Hello"));
}

 

 

 

4, Common regular expressions

Expression

describe

^[a-z0-9_-]{3,16}$

User name

^[a-z0-9_-]{6,18}$

Password

^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$

Mail box

[\u4e00-\u9fa5]

Regular expressions matching Chinese characters

 

Case use:

  • Title:

Enter a string from the console. Use regular to do a series of operations on strings:

① Determine whether the string is a qualified user name

② Replace the number in the string with "×" sign. The regularity of the number: [0-9] or \ \ d

③ Cut the string into an array of strings according to the "ා" sign and traverse the display

public static void main(String[] args) {
    //1. Console receive string
    Scanner scan = new Scanner(System.in);
    System.out.println("The console enters a string:");
    String username = scan.nextLine();
    //2, judgement
    String reg = "^[a-z0-9_-]{3,16}$";
    System.out.println(username.matches(reg)?"qualified":"Unqualified");
    //3, replace
    username = username.replaceAll("\\d","#");
    //4, cut
    String[] arr = username.split("#");
    System.out.println(Arrays.toString(arr));
}

 

 

In the next part, I'll go into the details of < regular expression - appendix > which is more important!

Pay attention to the dynamic in real time!!!

 

 

Please give yourself a compliment!

Make a little progress every day`~~~~~

Posted by jviney on Sat, 04 Apr 2020 10:00:42 -0700