Basic Rules of Regular Expressions

Table of contents

Just wrote some basic

1. Metacharacters

Two, antonyms

Three, limited characters

4. Escape characters

5. Character branches

Six, character grouping


Just wrote some basic

1. Metacharacters

Metacharacters: characters with specific meanings, common metacharacters are as follows

Common Metacharacters

the code illustrate example
.

matches any character except newline

1,n,*+
\w Match letters or numbers or underscores or Chinese characters \w:a,1,_
\s matches any whitespace
\d matches any number ^\d\d\d$:123,456,753
\b start and end of any word
^ match the beginning of the string
$ matches the end of the string

Two, antonyms

Antonym characters: mostly used to find any character except a certain character

Commonly used antonyms are as follows:

the code illustrate example
\W Match any character other than letters, numbers, underscores, and Chinese characters +,-
\S matches any character that is not a whitespace character
\D matches any character that is not a number s,.
\B Matches a position that is not the beginning or end of a word
[^x] matches any character except x [a]:b,s,c........

Three, limited characters

Limit characters are mostly used for repeated matching times

Commonly used limit characters are as follows

the code illustrate example
* Repeat zero or more times \d*: 0 or more times of numbers, such as 2, 3, 44, 555,, 4....
+ repeat one or more times \d+:1,11,222,333...
Repeat zero or one time \d?:1,,3
{n} repeat n times \d{2}:11,22,33,44
{n.} Repeat n or more times \d{3}:333,111,1111,55555
{n,m} Repeat nm times \d{3,4}111,1111,222,2222

4. Escape characters

In actual development, you may encounter a situation where you need to match metacharacters. At this time, you need to perform character escaping, such as metacharacters. * \ needs to be converted to \. \* \\

5. Character branches

Character branches are mostly used to meet the selection of different situations, use "|" to separate different conditions

The qq number is ten or eleven digits:

\d{10}|\d{11}

Six, character grouping

Character grouping is mostly used to repeat multiple characters, mainly by using parentheses () to group

(\d\w){3} repeat match 3 times (\d\w)

Guess you like

Origin blog.csdn.net/m0_51786204/article/details/130246807