PHP Regular Expressions get started with detailed

Source: Internet
Author: User
Tags learn php php regular expression
PHP Regular expression believe that a lot of small partners have a profound impact on this, the understanding of the small partners will feel that this thing is too difficult, do not know where to start, is a lot of experts on the PHP regular expression is smattering, then we will take you today from the PHP regular expression start to understand!

This article is a primer on the basics of PHP regular expressions to teach you how to learn PHP regular expressions, so that you can really master the PHP regular expression, interested in small partners to refer to

Mind Mapping

Introduced

Regular expression, everyone in the development should be often used, and now many development languages have regular expression of the application, such as JavaScript, Java,. Net, PHP, etc., I today on the regular expression of my understanding with you chatter, improper place, please advice!

The terminology you need to know--what do you know about the terms below?

Δ delimiter
Δ character Field
Δ modifier
Δ Qualifier
Δ off character
Δ wildcard character (forward pre-check, reverse pre-check)
Δ Reverse Reference
Δ Lazy Matching
Δ Comment
Δ 0 Characters justifies

Positioning

When do we use regular expressions? Not all characters are used as regular, and PHP in some ways affects efficiency. When we encounter the parsing of complex text data, it is a better choice to use regular.

Advantages

Regular expressions, when dealing with complex character operations, can increase productivity and save your code in a certain amount.

Disadvantages

When we use regular expressions, complex regular expressions increase the complexity of the code and are difficult to understand. So we sometimes need to add comments inside the regular expression.

Common mode

¤ delimiter, usually using "/" as the delimiter to start and end, you can also use "#".
When do you use "#"? Usually there is a lot of "/" characters in your string, because this character needs to be escaped, such as a 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 a string that matches the entire pattern.

The code for using the "#" delimiter is as follows. At this time the "/" 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 regular expressions.

We see ('/^http:\/\/[\w.] +) \/([\w]+]/([\w]+) \.html/i ') The last "I" is the modifier, which means ignoring the case, and one we often use is "x" for ignoring 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 Value field: [\w] The part that expands with square brackets is the field of characters.

¤ Qualifiers: such as [\w]{3,5} or [\w]* or [\w]+] the symbols after these [\w] represent qualifiers. The specific significance is introduced.

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

* Represents 0 to more

+ represents 1 to more.

¤ Caret character

^:

> In Word Value field (e.g. [^\w]) for negation (not included)--"Reverse selection"

> precedes the expression, indicating that it starts with the current character. (/^n/i, which means start with N).

Note that we often call "\" called "Skip character". Used to escape some special symbols, such as ".", "/"

wildcard character (Lookarounds): asserts that some characters in some strings exist or not!

There are two kinds of lookarounds: Lookaheads (forward pre-check? =) and Lookbehinds (reverse pre-check? <=).
> Format:
Forward Pre-check: (? =) corresponds to (?!) To express a negative meaning
Reverse Pre-check: (? <=) corresponds to (? <!) To express a negative meaning
Character immediately before and after

$regex = '/(? <=c) d (? =e)/'; /* immediately preceding C, D followed by e*/$str = ' abcdefgk '; $matches = Array (); if (Preg_match ($regex, $str, $matches)) {var_dump ($matches);} echo "\ n";

Negative meaning:

$regex = '/(? <!c) d (?! e)/'; /* d not immediately following C, D is not immediately followed by e*/$str = ' abcdefgk '; $matches = Array (); if (Preg_match ($regex, $str, $matches)) {var_dump ($matches);} echo "\ n";

> Character width: 0
Verifying the 0-character code

$regex = '/he (? =l) lo/i '; $str = ' HELLO '; $matches = Array (); if (Preg_match ($regex, $str, $matches)) {var_dump ($matches);} echo "\ n";

Can't print the results!

$regex = '/he (? =l) llo/i '; $str = ' HELLO '; $matches = Array (); if (Preg_match ($regex, $str, $matches)) {var_dump ($matches);} echo "\ n";


Can print out the results!

Description: (? =l) means that he is followed by an L character. But (? =l) itself does not account for characters, to be distinguished from (l), (L) itself as a character.

Capturing data

Groups that do not have a specified type will be acquired for later use.
> indicates that a type refers to a wildcard character. So only the parentheses start position without a question mark can be captured.
> A reference within the same expression is called a reverse reference.
> Call format: \ number (such as \1).

$regex = '/^ (Chuanshanjia) [\w\s!] +\1$/'; $str = ' Chuanshanjia thank Chuanshanjia '; $matches = Array (); if (Preg_match ($regex, $str, $matches)) {var_dump ($matches);} echo "\ n";

> Avoid capturing data
Format: (?:p Attern)
Pros: The number of valid reverse references is kept to a minimum, and the code is more and clearer.

> Named capturing groups
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";

Run results

Lazy Matching (remember: There are two operations, see the principle section below)

Format: Qualifier?

Principle: "?" : The smallest data is used if there is a qualifier in front of it. such as "*" will take 0, and "+" will take 1, if it is {3,5} will take 3.

Let's 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";

Results 2

Code 3, using "+"

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

Results 3

Code 4, using {3,5}

<?php$regex = '/hel{3,10}?/i '; $str = ' hellllllllllllllll '; if (Preg_match ($regex, $str, $matches)) {Var_dump ($ matches);} echo "\ n";


Results 4

Comments for regular expressions

Format: (? # comment Content)
Purpose: Mainly used for complex annotations

Contribution code: is a regular expression used to connect to the MySQL database

$regex = '/^host= (? <!\.) ([\d.] +)(?! \.)     (? #主机地址) \| ([\w!@#$%^&* () _+\-]+)      (? #用户名) \| ([\w!@#$%^&* () _+\-]+)      (? #密码) (?! \|) $/ix '; $str = ' host=192.168.10.221|root|123456 '; $matches = Array (); if (Preg_match ($regex, $str, $matches)) {var_dump ($matches);} echo "\ n";

Special characters

Summarize:

After reading this article and have some understanding, then can be said to be the PHP regular expression introduction, but only to learn this article is far enough, want to learn PHP regular expression, but also need to learn more!

Related recommendations:

Syntax of regular expressions in PHP


PHP Regular Expression Application


php Regular expression

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.