PHP Regular Expression basics, php Regular Expression Basics

Source: Internet
Author: User
Tags php regular expression

PHP Regular Expression basics, php Regular Expression Basics

Mind Map

Introduction

Regular Expressions are frequently used in development. Many development languages now have regular expressions, such as JavaScript, Java, and ,. net, PHP, etc. I will share my understanding of regular expressions with you today. For more information, please advise!

What terms do you need to know-what terms do you know below?

Delta delimiters
Delta character field
Delta Modifier
△Qualifier
△Delimiters
△Wildcard (Forward pre-query, reverse pre-query)
△Reverse reference
△Inert match
Delta Annotation
△0 character width

Positioning

When should we use regular expressions? 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.

Advantages

When processing complex character operations, regular expressions can improve the efficiency and reduce the amount of code.

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

¤, Which generally uses "/" as the start and end of the ing. You can also use "#".
When can I use? Generally, when your string contains many "/" characters, such characters need 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 "#" separator 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] + )\. the last "I" in html/I ') is the modifier, which indicates case-insensitive, 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: "\" 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 (?!) Negative meaning
Reverse pre-query :(? <=) Corresponds (? <!) Negative meaning
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
Verify the zero-character 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.

Capture Data

Groups that do not specify the type 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
Format :(? : Pattern)
Advantage: The number of valid reverse references will be minimized, and the code will be clearer.

> Named 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 minimum 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:

Regular Expression comments

Format :(? # Comment)
Purpose: it is mainly used for complex annotations.

Contributed 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

If you want to learn php Regular Expressions well, learning this article is far from enough. I hope you will continue to learn and read articles related to php regular expressions.

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.