Frequently used PHP regular expression

Source: Internet
Author: User
Mind map introduces regular expressions, which are 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... "> <LINKhref =" http://www.php100.com//statics/st

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. 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?

Delimiters; character domains; modifiers; delimiters; wildcards (forward and reverse pre-queries); reverse references; inert matches; annotations; zero 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

The delimiters. generally, "/" is used as the start and end of the delimiters. 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] + )\. the last "I" in html/I ') is a modifier, indicating that the case is ignored. another commonly used one is "x", which indicates that spaces are ignored.

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.

Qualifier: for example, [\ w] {3, 5}, [\ w] *, or [\ w] + all the symbols after [\ w] indicate the qualifier. 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. * Indicates 0 to multiple, and + indicates 1 to multiple.

Escape character

Put it in the character field (for example, [^ \ w]) to indicate a negative (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 (?!) Negative meaning. reverse pre-query :(? <=) Corresponds (?

Followed by characters

$ Regex = '/(? <= C) d (? = E)/';/* d followed by c, and d followed by e */

$ Str = 'abcdefg ';

$ Matches = array ();

If (preg_match ($ regex, $ str, $ matches )){

Var_dump ($ matches );

}

Echo "\ n ";

Negative meaning:

$ Regex = '/(?

$ Str = 'abcdefg ';

$ Matches = array ();

If (preg_match ($ regex, $ str, $ matches )){

Var_dump ($ matches );

}

Echo "\ n ";

 

Character width: Zero

Verify zero-character generation

$ 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.

Naming capture group

Format :(? P <组名> ) Call method (? P = group name)

$ Regex = '/(? Pchuanshanjia) [\ 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.

$ Regex = '/hel*/I ';

$ Str = 'hellllllllllllllllll ';

If (preg_match ($ regex, $ str, $ matches )){

Var_dump ($ matches );

}

Echo "\ n ";

Result 1.

Code 2

$ Regex = '/El *? /I ';

$ Str = 'hellllllllllllllllll ';

If (preg_match ($ regex, $ str, $ matches )){

Var_dump ($ matches );

}

Echo "\ n ";

Result 2:

Code 3: Use "+"

$ Regex = '/El +? /I ';

$ Str = 'hellllllllllllllllll ';

If (preg_match ($ regex, $ str, $ matches )){

Var_dump ($ matches );

}

Echo "\ n ";

Result 3:

Code 4: Use {3, 5}

$ Regex = '/El {3, 10 }? /I ';

$ Str = 'hellllllllllllllllll ';

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 = (? \ |
([\ W! @ # $ % ^ & * () _ + \-] + )(? # Username)
\ |
([\ W! @ # $ % ^ & * () _ + \-] + )(? # Password)
(?! \ |) $/Ix ';

$ Str = 'host = 192.168.10.221 | root | 100 ';
$ Matches = array ();

If (preg_match ($ regex, $ str, $ matches )){
Var_dump ($ matches );
}

Echo "\ n ";

 

Link: http://www.cnblogs.com/baochuan/archive/2012/03/12/2391135.html

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.