1. Introduction to regular expressions

A regular expression is a matching rule described by a string. You can use a regular expression to quickly determine whether a string matches the rule. The regular expression itself has nothing to do with the language. It is a kind of rule. However, the implementation of regular expression depends on different languages.

The regular expression in the work is often used in E-mail format validation id, phone number verification, validation, etc., of course not necessarily achieve these functions depends on a regular (hereinafter in this paper, we use regular instead of a regular expression), but we can be easily done through regular this a series of functions, such as: we judge whether a string is a number (0-9) :

Instead of using the re, we could write this in code

public boolean isNumber(String s){
    for(int i=0; i<s.length(); i++){char c=s.charAt(i);
        if(c<'0'||>'9') {return false; }}return true;
}
Copy the code

Check with the re:

public boolean isNumber(String s){
    return s.matchers("^\\d+$");
}
Copy the code

As we can see here, the number of lines of code required to implement the function with the re is very small. We just need to write a suitable re to implement the verification function through the API. Let’s look at another example: replace a space in a string with *_*

public static void main(String[]args){
    String s="hello world !";
    s=s.replaceAll("\\s"."* _ *");
    System.out.println(s);       
}
Copy the code

Final output:

hello*_*world*_*!
Copy the code

With these two examples we have a brief introduction to regex, so let’s get started.

2. Simple matching

The re follows a left-to-right matching rule. For example, the re “ABC” matches the three characters “ABC” in the string. This is an exact match of the re. Of course, exact matching is not the desired result. We expect to be able to accurately match the set of strings represented by the re expression. This is where the re comes in. Let’s start with some common matching rules

Note: in Java, “\” represents an escape character, which is not a common string. However, “\” is a relatively common character in regex, which needs to be escaped with “\”. For example, “\s” is a re, which should be written as “\\s” in Java.

2.1 Matching Numbers

\d stands for matching any number from 0 to 9

  • “2” can match \d
  • “4” also matches \d
  • “A” does not match \d.

2.2 Matching Spaces

\s Matches a space (including Tab characters)

  • “A, b”, it matches

  • “Ab” does not match