PHP Regular Expression

Source: Internet
Author: User
Click the Mind Map to view the specific content!

Introduction to regular expressions, which should be frequently used in development. Many development languages now have regular expressions, such as JavaScript, Java, and ,. net, PHP, etc. Today I will share my understanding of regular expressions with you. For more information, please advise! What terms do you need to know-what terms do you know below? Delta delimiters Delta Character Domain Delta modifier Delta delimiters Delta wildcard (Forward pre-query, reverse pre-query) when do we use a regular expression to locate the reverse reference of Delta-inert-matching Delta-annotation △0-character width? Not all character operations use regular expressions, but PHP uses regular expressions in some ways, which affects efficiency. Regular Expressions are a good choice when parsing complex text data. Advantage regular expressions can improve the work efficiency and reduce the amount of code when processing complex character operations. Disadvantages when we use regular expressions, complex regular expressions increase the complexity of the Code, making it hard to understand. So sometimes we need to add comments inside the regular expression. General Mode

The delimiter. "/" is usually used as the delimiter to start and end. You can also use "#". When can I use? Generally, when your string contains many "/" characters, this character needs to be escaped during regular expressions, such as Uri. The code for using the "/" Delimiter is as follows.
$regex = '/^http:\/\/([\w.]+)\/([\w]+)\/([\w]+)\.html$/i';$str = 'http://www.youku.com/show_page/id_ABCDEFG.html';$matches = array();if(preg_match($regex, $str, $matches)){    var_dump($matches);}echo "\n";

$ Matches [0] In preg_match will contain strings that match the entire pattern.

The code for using the "#" Delimiter is as follows. At this time, "/" is not escaped!

$regex = '#^http://([\w.]+)/([\w]+)/([\w]+)\.html$#i';$str = 'http://www.youku.com/show_page/id_ABCDEFG.html';$matches = array();if(preg_match($regex, $str, $matches)){    var_dump($matches);}echo "\n";

Modifier: used to change the behavior of a regular expression.

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.

Contribution code:

$regex = '/HELLO/';$str = 'hello word';$matches = array();if(preg_match($regex, $str, $matches)){    echo 'No i:Valid Successful!',"\n";}if(preg_match($regex.'i', $str, $matches)){    echo 'YES i:Valid Successful!',"\n";}

 

Character field: [\ W] the expanded part in square brackets is the character field.

Token qualifier: for example, [\ W] {3, 5}, [\ W] *, or [\ W] + the symbols after these [\ W] indicate the delimiter. This section describes the specific meaning.

{3, 5} represents 3 to 5 characters. {3,} is more than 3 characters, {, 5} is up to 5 characters, and {3} is three characters.

* 0 to multiple

+ Indicates one or more.

Escape Character

^:

> Put it in the character field (for example, [^ \ W]) to indicate no (excluding the meaning)-"reverse selection"

> Put it before the expression to start with the current character. (/^ N/I, which indicates starting with N ).

Note that "\" is often called "Escape Character ". Used to escape some special symbols, such ".","/"

 

Wildcard (lookarounds): determines whether some characters in some strings exist! Lookarounds can be divided into two types: lookaheads (Forward pre-query? =) And lookbehinds (reverse pre-query? <=).> Format: Forward pre-query :(? =) Corresponding (?!) Indicates negative meaning reverse pre-query :(? <=) Corresponds (? <!) Indicates that the negative meaning is followed by characters
$ RegEx = '/(? <= C) D (? = E)/';/* D followed by C, D followed by E */$ STR = 'abcdefg'; $ matches = array (); If (preg_match ($ RegEx, $ STR, $ matches) {var_dump ($ matches);} echo "\ n ";

Negative meaning:

$ RegEx = '/(? <! C) D (?! E)/';/* D is not followed by C, and D is not followed by E */$ STR = 'abcdefg'; $ matches = array (); if (preg_match ($ RegEx, $ STR, $ matches) {var_dump ($ matches);} echo "\ n ";

 

> Character width: Zero-character Verification Code
$regex = '/HE(?=L)LO/i';$str = 'HELLO';$matches = array();if(preg_match($regex, $str, $matches)){    var_dump($matches);}echo "\n";

No result is printed!

$regex = '/HE(?=L)LLO/i';$str = 'HELLO';$matches = array();if(preg_match($regex, $str, $matches)){    var_dump($matches);}echo "\n";

Can print the result!

Note :(? = L) It means that he is followed by an L character. But (? = L) it does not take up any character. It must be differentiated from (L). (l) itself occupies one character.

If the captured data is not grouped by a specified type, it will be obtained for future use.> The specified type indicates a wildcard. Therefore, only the starting position of parentheses without question marks can be captured.

> References in the same expression are called reverse references.> Call format: \ number (for example, \ 1 ).
$regex = '/^(Chuanshanjia)[\w\s!]+\1$/';    $str = 'Chuanshanjia thank Chuanshanjia';$matches = array();if(preg_match($regex, $str, $matches)){    var_dump($matches);}echo "\n";

 

> Avoid data capture formats :(? : Pattern) Advantages: The number of valid reverse references is kept at least, and the code is clearer. > Name capture group format :(? P <Group Name>) Call method (? P = group name)
$regex = '/(?P<author>chuanshanjia)[\s]Is[\s](?P=author)/i';$str = 'author:chuanshanjia Is chuanshanjia';$matches = array();if(preg_match($regex, $str, $matches)){    var_dump($matches);}echo "\n";

Running result

Inertia matching (remember: two operations will be performed. Please refer to the following principles)

Format: qualifier?

Principle :"? ": If there is a qualifier before, the smallest data will be used. For example, "*" takes 0, and "+" takes 1. For example, if it is {3, 5}, it takes 3.

Let's take a look at the following two codes:

Code 1.

<?php$regex = '/heL*/i';$str = 'heLLLLLLLLLLLLLLLL';if(preg_match($regex, $str, $matches)){    var_dump($matches);}echo "\n";

Result 1.

 

Code 2

<?php$regex = '/heL*?/i';$str = 'heLLLLLLLLLLLLLLLL';if(preg_match($regex, $str, $matches)){    var_dump($matches);}echo "\n";

Result 2:

 

Code 3: Use "+"

<?php$regex = '/heL+?/i';$str = 'heLLLLLLLLLLLLLLLL';if(preg_match($regex, $str, $matches)){    var_dump($matches);}echo "\n";

Result 3:

Code 4: Use {3, 5}

<?php$regex = '/heL{3,10}?/i';$str = 'heLLLLLLLLLLLLLLLL';if(preg_match($regex, $str, $matches)){    var_dump($matches);}echo "\n";

Result 4:

Comment format of Regular Expressions :(? # Annotation content) Purpose: Mainly used for complex annotation contribution code: a regular expression used to connect to the MySQL database
$ RegEx = '/^ host = (? <! \.) ([\ D.] + )(?! \.)(? # Host address) \ | ([\ W! @ # $ % ^ & * () _ + \-] + )(? # Username) \ | ([\ W! @ # $ % ^ & * () _ + \-] + )(? # Password )(?! \ |) $/IX '; $ STR = 'host = 192.168.10.221 | root | 123456'; $ matches = array (); If (preg_match ($ RegEx, $ STR, $ matches) {var_dump ($ matches);} echo "\ n ";

 

Special characters

Special characters Explanation
* 0 to multiple times
+ 1 to multiple times can also be written as {1 ,}
? 0 or 1 time
. Match All single characters except line breaks
\ W [A-zA-Z0-9 _]
\ S Blank characters (space, line break, carriage return) [\ t \ n \ r]
\ D [0-9]
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.