Regular expression in php (I) basics
Regular expressions are a difficult part for php beginners. after a long time, they often forget the functions of various symbols. However, after a summary, you will find that the memory is not very difficult.
Perl is a common regular expression-compatible function. the general format is (preg _), which is also described in this article.
Learning Regular expressions is nothing more than learning 1. writing patterns
2. Regular expressions + functions = powerful character processing functions.
First, we understand the basic knowledge.
1. the atomic name is the minimum unit of the string. It includes two types:
① Printable characters are generally characters without special meanings.
② Non-printable characters, representing the range:
\ D: any decimal number
\ D: a character other than any number
\ S: any blank character
\ S: any non-blank character
A-zA-Z0-9 \ w
\ W: any character except a-zA-Z0-9
(Tips: these are usually just a few, which are easy to remember, remember every abbreviation (d is digital, s is word, s is space, and its meaning is also very clear), and the upper case will always be opposite to the lower case)
2. Custom atomic table ([])
[] Character class, matching any atom, one square brackets only match one character
The opposite [^] Exclusion character, excluding any character in square brackets
[-] Indicates a certain range. for example, [a-z] indicates any character from a to z.
Learn more about several common characters.
1. delimiters (determined to be regular rather than regular string symbols) (| /......)
Definition: all symbols except letters, numbers, and forward slashes \ are the delimiters. they are generally represented by backslash (/).
Format:/tm/
2. Periods (.)
Match any character other than the line break.
A question can interpret this effect well and write several words starting with s and ending with t.
/^ S ** t $/^ s * t /......
3. the qualifier (? * + {N, m })
? : Match the first character 0 or 1 time
+: Match the first character 1 or multiple times
*: Match the first character 0 or multiple times
{N}: match the first character n times
{N ,}: match the first character at least n times
{N, m}: match the first character at least n times, at most m times
4. select the character (|)
For example:/cat | dog/matches a cat or a dog
5. parentheses (())
() Has many functions, including new functions generated when used together with functions
1. higher priority
2. use as a big atom
3. use the most child mode (\ 1 takes the first child mode, \ 2 takes the second mode ...)
"\ 1" '\ 1'