PHP regular expressions-PHP source code

Source: Internet
Author: User
This article introduces the usage of regular expressions. Today, let's take a look at the basic usage of common Regular Expressions in php. If you have any need, refer to the following. This article introduces the usage of regular expressions. Today, let's take a look at the basic usage of common Regular Expressions in php. If you have any need, refer to the following.

Script ec (2); script

Reference

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?

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


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.

The Code 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!

The Code 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 ";


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

We can see ('/^ http: // ([w.] +)/([w] +)/([w]+).html/I ') the last "I" is a modifier, indicating that the case is ignored, we also often use "x" to ignore spaces.

Contribution code:

The Code is as follows:

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


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

* 0 to multiple

+ Indicates one or more.


Escape Character

^:

> Put it in the character field (for example, [^ w]) to represent 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: We often choose "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

The Code is as follows:

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

The Code is as follows:

$ Regex = '/(? $ Str = 'abcdefg ';
$ Matches = array ();

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

Echo "n ";


> Character width: Zero
Verify the zero-character code

The Code is as follows:

$ Regex = '/HE (? = L) LO/I ';
$ Str = 'hello ';
$ Matches = array ();

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

Echo "n ";

No result is printed!

The Code is as follows:

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

The Code is as follows:

$ Regex = '/^ (Chuanshanjia) [ws!] + 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 <组名> ) Call method (? P = group name)

The Code is as follows:

$ 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: Match "? "The previous part, and then match the expression on the right. If the expression on the right matches successfully, the entire match ends.

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

Code 1.

The Code is as follows:

$ Regex = '/(") [^ 1] + 1/I ';
$ Str = '"a" "B" "c" "d "';
$ Matches = array ();

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

Echo "n ";

Result 1.

Code 2

The Code is as follows:

$ Regex = '/(") [^ 1] +? 1/I ';
$ Str = '"a" "B" "c" "d "';
$ Matches = array ();

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

Echo "n ";

Result 2:

Analysis:
Compare two regular expressions: the first one is added "? ", The second one does not exist.
Results: The first parameter is used to print all characters. The second parameter only prints "" "".
Conclusion:
> First, if (") [^ 1] + 1 conditions are met
"A", "a" "B", "a" B "c", "a" B "" c "" d "," B ", "B" "c", "B" "c" "d", "c", "c" "d", "d"
However, the First Regular Expression selects the largest "a" "B" "c" "d", indicating that the non-inert match will compare the largest matching result.

> The Second Regular Expression: First matches (") [^ 1] +. If the match succeeds, we are matching"? ". 1 On the right. If the match is successful, the entire match ends.

Other cases:

The Code is as follows:
"Oh," my "God" ====>/ (") ([^ 1] | \ 1 )*? (?


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

The Code is as follows:

$ 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 ";

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) [tnr]
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.