Simple knowledge of regular expressions

 

1. Ordinary characters: letters, numbers, Chinese characters, underscores and other characters that have no special meaning, match a character corresponding to it when matching (single character matching).

 

2. Escape character:

 

Note: The last line of the table is the special meaning in the regular expression. When matching symbols , add \ 

 

3. Character set:

 

 

Note: It can be matched with multiple characters, and the corresponding uppercase means the opposite meaning (for example: \D means not a number)

 

4. Custom character set (square brackets [] means that any one of the characters can be matched , and the relationship is "or" )

 

Note: When special symbols are included in square brackets [], they will lose their meaning, except for ^ and-which are special;

1. When ^ is placed at the top, it means that it is not as above [^abc] means to match any character other than a, b, c, but if you put ^ in the middle, such as [a^b], it means to match a or ^ Or b. Therefore, as long as ^ is not placed at the top, it will not mean the meaning of non. If you must put ^ at the top, you can add \ to it if it does not mean not. For example, [\^ab] means matching ^ or a or b A character.

2. The character-is almost the same as ^, as long as the-is not placed in the middle of the character, it does not indicate the range. If it must be placed in the character, add \

For example, [f\-k] means to match f or-or f.

 

 

5. Quantifier ({} is placed after the expression):

 

Example: [a\-b]{2} means that the expression [a\-b] repeated twice can be matched, such as: aa acc -b m -a aa b

           [a\-b]{2,3} means that the expression [a\-b] repeated two to three times can be matched, such as: aaa cc -b m -a aab

Note: When using {m,n} quantifiers, the default is greedy mode (such as: [a\-b]{1,2} When matching the string aaaaa , he will use as many matching methods as possible, namely greedy mode aa aa a which is to match two aa aa re-match two matches and then a a, rather than the first match and then a a a a a a ... and that is a a a a a. I said earlier, one after another match Is called non-greedy mode, that is, to match as few characters as possible, namely a a a a a, you only need to add characters after {}? That is [a\-b]{1,2}?.? + * Cannot be added after? )

 

 

 

 

6. Character boundary (zero width)

 

 

 

Example: ^ab matches a string

ab dagdaffdafewhf

abg hf

Treat the entire string as a string and match the first ab

Similarly, hf$ matches to the last hf

Usage of \b: It can be understood as a virtual position and the characters before and after the position must have at most one \w (character underscore number) 

 

 

 

 

7. Selection and grouping

 

 

 

 

8. Zero-width assertion (the following expression just indicates a position, that is, what should be before or after the position of the exp expression)

 

 

Example: \w+(?=ing) match string: go ing do ing play ing match string ending in ing (red part)

           \w+(?!=ing) In contrast to the above, the matching string does not end with ing

          (?<=go)ing matching string: go ing doing playing matches the string that starts with go and ends with ing (red) (?<!go)ing On the contrary, matches the string that does not start with go and ends with ing. going do ing play ing

 

 

 

 

 

 

 

 

 

                                                                                                                                        (Part of this article comes from online video summary)

Guess you like

Origin blog.csdn.net/weixin_41237676/article/details/84400854