\x[0-9A-Fa-f]{1,2} | |
|
See PHP Manual for details on this part. Here is a brief description:
In single quotes, escape single quotes (') and backslashes (\), and output the original meaning of other characters;
In double quotes, special characters except double quotes (\") are escaped and variables are parsed.
Therefore, in these two common string definitions, backslashes are Bars (\) always need to be escaped.
PHP Regular Expressions
PHP provides two sets of regular expression function libraries: [Reference]
One set is Provided by the PCRE (Perl Compatible Regular Expression) library. Use functions named with the prefix "preg_";
A set of functions provided by POSIX (Portable Operating System Interface of Unix) extensions (PHP default). Use "ereg_" Function named for the prefix;
Regular expressions provide the functions of matching, replacing, and splitting.
Currently only perl-style regular functions are used. The following is only for this type of strings Description.
Perl-style regular expressions are required to be contained in delimiters ("/" or "#"), as follows: [Reference]
$str = 'http://www.youku.com/show_page/id_ABCDEFG.html';
$regex = '/^http:\/\/([\w.]+)\/([\w]+)\/([\w]+)\.html$/i';
$regex = '#^http://([\w.]+)/([\w]+)/([\w]+)\.html$#i';
Copy after login
In order to split a URL, the $regex variable defines a regular expression string:
^http://([\w.] )/([\w] )/([\w ] ).html$
This string contains some special characters, such as "/" and ".", which need to be escaped and defined with backslash ("\") in Perl style, as follows :
^http:\/\/([\w.] )\/([\w] )\/([\w] )\.html$
At the same time this character The string needs to be included in the delimiter, so there are the above two types of expressions; if "#" is used as the delimiter, the "/" does not need to be escaped.
Regular Expression escaping
In regular expressions, if you want to match the following single characters, you need to use backslash ("\") to escape:
"\", "? ","*","^","$"," ","(",")","|","{","["
In Perl-style regular expressions , if it matches the following single character, or a single character not matched above, it also needs to be escaped with a backslash ("\"):
^=}]/:<>.'"
Summary
To write a correct Perl-style regular expression string, three steps are required:
Write a correct regular expression and pay attention to the conversion of special characters Definition
Put it into the delimiter and escape it using Perl-style escaping rules
Escape the content of the above string according to the way PHP string definition