Perl is a common compatible regular expression function, the general form is (preg_), which is what this article mainly introduces.
Learning regular rules is nothing more than learning 1. Writing patterns
2. Regular function = powerful character processing function.
Let’s cover the basics first.
1. Atom, as you can tell from the name, is the smallest unit of a string. It includes two types:
① Printable characters are characters that generally have no special meaning.
②Unprintable characters, representative range:
d: represents any decimal number
D: represents any character other than a number
s: represents any blank character
S: represents any non-whitespace character
w:a-zA-Z0-9
W: Any character except a-zA-Z0-9
(Tips: These are usually the only ones. It’s easy to remember. Just remember that each one has its English abbreviation (d stands for digital, s stands for word, and s stands for space. The meaning is also very clear), and the capital letters are always Will fight with lower case)
2. Customized atom table ([])
[]Character class, matches any one of the atoms, a square bracket matches only one character
The opposite [^] excludes characters and excludes any character in square brackets
[-] represents a certain range, for example: [a-z] represents any character from a to z
Learn more about some common characters.
1. Delimiter (a symbol that makes sure it is a regular string rather than an ordinary string) (| / ...)
Definition: Any symbol other than letters, numbers, and forward slashes is a delimiter symbol. Generally, a backslash (/) is used to represent
Shape like: /tm/
2. Period character (.)
Can match any character except newline character.
A question that can explain this effect well is to write several words starting with s and ending with t.
/^s**t$/ /^s*t/ ……
3. Qualifier (?* {n,m})
? : Matches the previous character 0 or 1 times
: Matches the previous character 1 or more times
* : Matches the previous character 0 or more times
{n}: Match the previous character n times
{n,}: Match the previous character at least n times
{n,m}: Match the previous character at least n times and at most m times
4. Select the character (|)
For example: /cat|dog/ matches cat or dog
5. Bracket characters (())
() has many functions, including new functions produced when used together with functions
1. Increase priority
2. Used as a large atom
3. Used as the sub-mode (1 takes the first sub-mode, 2 takes the second mode...)
"\1" '1'