Commonly used regular expressions

Preface

Usage scenarios:
1. Check the validity of the data
2. Find text that meets the requirements and perform operations such as cutting and replacing the text

Commonly used knowledge points are listed below:

Metacharacters

Concept: refers to special characters with special meanings in regular expressions

Insert image description here

Special single character

Insert image description here
. Any character
Example:
Insert image description here
\d represents a number \D represents a non-digit
Insert image description here
Insert image description here
\w Numbers and letters underline \W negate
Insert image description here
Insert image description here
\s whitespace character \S negate

Insert image description here
Insert image description here
Extend other whitespace characters
Insert image description here

quantifier

Meaning: The single characters and whitespace characters mentioned above can only match single characters. When you need to match multiple things, you need to use quantifiers.
Insert image description here

scope

It is needed when special single characters cannot meet the needs.

Insert image description here

Make a common exercise based on these: match mobile phone number
1, 11 digits
2, the first digit starts with 1
3, the second digit may be 3, 4, 5, 6, 7, 8, 9;
Insert image description here

Group

When there are multiple metacharacters that make up a part and should be treated as a whole, you need to wrap it with ()

Another question:

Matches those starting with http:// https:// or ftp://

Insert image description here

Matching patterns and escaping

Case insensitive
(?i) cat

Insert image description here
Insert image description here
When special single characters are to be matched correctly, they need to be escaped with \
Insert image description here
Insert image description here
Insert image description here

affirmation

Solving word boundary problems

Insert image description here
Insert image description here

Extension regarding performance issues

https://jishuin.proginn.com/p/763bfbd69152

at last

Although regular expressions are good, they need to be used with caution, especially when searching in large text.

Guess you like

Origin blog.csdn.net/weixin_45485922/article/details/119385763