PHP Regular Expressions Getting Started tutorial (recommended), regular expression Getting Started Tutorial _php tutorial

Source: Internet
Author: User
Tags php regular expression

PHP Regular Expressions Getting Started tutorial (recommended), regular expression Getting started Tutorial


Mind Mapping

Click, you can see the specific content!

Introduced

Regular expression, everyone in the development should be often used, now a lot of development languages have regular expression of the application, such as javascript,java,.net,php, and so on, I today on the regular expression of my understanding with you chatter, improper place, please advice!

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 SUCCESSF ul! ', ' \ 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 the (?

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 = '/(?
 

> 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 <组名> ) call mode (? 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";

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= (?
  

Special characters

Special characters Explain
* 0 to several times
+ 1 to multiple can also be written as {1}
? 0 or 1 times
. Match all the individual characters except the line break
\w [A-za-z0-9_]
\s White space characters (spaces, line breaks, carriage returns) [\t\n\r]
\d [0-9]

The above is a small series to introduce you to the PHP regular expression of the relevant knowledge of the tutorial, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Thank you very much for the support of our website!

http://www.bkjia.com/PHPjc/1127855.html www.bkjia.com true http://www.bkjia.com/PHPjc/1127855.html techarticle PHP Regular expression Getting Started tutorial (recommended), the regular expression get started Tutorial mind map Click, you can see the specific content! Introduce regular expression, everyone in the development should be often used ...

  • Related Article

    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.