1. Select the PHP Regular Expression processing function library
There are two commonly used functions. One is a Perl-based function that uses the prefix "prefix _". The expressions should be included in the delimiters, such as diagonal lines (/"),
The other is a regular expression in the POSIX (portable Operation System Interface) Extension syntax, starting with "ereg. Preg _.
Common functions:
Preg_match ()
Preg_match_all ()
Preg_replace ()
Preg_split ()
Preg_grep ();
Preg_replace_callback ()
2. Regular Expression composition:
From atoms (such as A to Z), metacharacters (*, + ,?) And pattern correction.
3. Regular Expression metacharacters:
The Regular Expression Language consists of two basic character types: literal (normal) text characters and metacharacters. Metacharacters enable the regular expression to process. Metacharacters can be any single character in [] (for example, [a] indicates matching a single lowercase character ), it can also be a character sequence (for example, [a-d] indicates matching any character between A, B, C, and D, while \ W indicates any English letter, number, and underline ), below are some common metacharacters:
. Match any character except \ n (note that the metacharacter is a decimal point ).
[ABCDE] matches any character in ABCDE.
[A-H] matches any character between A and H.
[^ Fgh] does not match any character in fgh
\ W matches any one of the upper and lower case English characters and numbers 0 to 9 and underline, equivalent to [a-zA-Z0-9 _]
\ W does not match any one of the upper and lower case English characters and numbers 0 to 9, equivalent to [^ a-zA-Z0-9 _]
\ S matches any blank characters, which is equivalent to [\ f \ n \ r \ t \ v]
\ S matches any non-blank characters, which is equivalent to [^ \ s]
\ D matches a single number between 0 and 9, which is equivalent to [0-9]
\ D does not match any single number between 0 and 9, which is equivalent to [^ 0-9]
[\ U4e00-\ u9fa5] matches any single Chinese character (here it uses unicode encoding to represent Chinese characters)
4. Use "\" to cancel the meaning of some special characters, such as "", "*", "+". If it is used as an atom, must be used like \ ", \ *, \ +.
5. modifier: Case Sensitive
We can see ('/^ http: \/([\ W.] +) \/([\ W] +) \/([\ W] + )\. html/I') The last "I" in is the modifier, indicating that the case is ignored, and "X" is often used to ignore spaces.
◆ I: if "I" is added to the modifier, the regular expression will be case insensitive, that is, "a" and "A" are the same.
◆ M: The default regular start "^" and end "$" only if "M" is added to the modifier of the regular string ", the start and end operations refer to each line of the string. Each line starts with "^" and ends with "$ ".
◆ S: If "S" is added to the modifier, the default "." indicates that any character except the line break will become any character, that is, include a line break!
◆ X: If this modifier is added, the blank characters in the expression will be ignored unless it has been escaped.
◆ E: This modifier is only useful for replacement, which indicates that it is used as PHP code in replacement.
◆ A: If this modifier is used, the expression must be the start part of the matched string. For example, "/a" matches "ABCD ".
◆ E: opposite to "M". If this modifier is used, "$" matches the end of an absolute string instead of the line break. This mode is enabled by default.
◆ U: similar to the question mark, used to set "greedy mode ".
6. Regular Expression qualifier
The above metacharacters are matched for a single character. To match multiple characters at the same time, you also need to use a qualifier. Below are some common delimiters (N and m in the following table both represent integers and 0 <n <m ):
* Matches 0 to multiple metacharacters, equivalent to {0 ,}
? Matches 0 to 1 metacharacters, equivalent to {0, 1}
{N} matches n metacharacters
{N,} matches at least N metacharacters
{N, m} matches n to M metacharacters
+ Match at least 1 metacharacters, equivalent to {1 ,}
\ B match word boundary
^ The string must start with a specified character
$ The string must end with a specified character