Home>

Regular expression,Everyone should use it frequently in development,Many development languages ​​now have applications for regular expressions.This article introduces you to php regular expressions tutorial,Interested friends study together

mind Mapping

Click on the picture below,You can see the specific content!

Introduction

Regular expression,Everyone should use it frequently in development,Many development languages ​​now have applications for regular expressions,For example, javascript, java, .net, php, etc. Today I will share my understanding of regular expressions with everyone,InappropriatePlease advise!

Positioning

When do we use regular expressions?Not all character operations are regular.In some ways, PHP uses regularity instead to affect efficiency.When we encounter parsing of complex text data,Using regular is a better choice.

advantage

When regular expressions deal with complex character operations,Can improve work efficiency,It also saves your code amount to a certain extent.

Disadvantage

When we use regular expressions,Complex regular expressions increase the complexity of the code,It's hard to understand.So sometimes we need to add comments inside the regular expression.

Common mode

¤ Delimiter, usually use "/" as the start and end of the delimiter,You can also use "#".

When to use "#"??Generally when there are many "/" characters in your string,Because regular characters need to be escaped,For example, uri.

The code 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 using the "#" delimiter is as follows.Don't escape the "/" at this time!

$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 the last of ("/^http:\/\/([\w.]+)\/([\w]+)\/([\w]+)\.html/i") An "i" is a modifier,Means ignore case,Another one we often use is "x" to ignore spaces.

Contributing 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 part enclosed by square brackets is the character field.

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

{3,5} means 3 to 5 characters. {3,} is more than 3 characters, {, 5} is a maximum of 5, and {3} is three characters.

* Means 0 or more

+ Means 1 or more.

¤ caret

^:

>Placed in a character field (eg:[^ \ w]) for negation (meaning not included)-"Reverse Selection"

>Put before the expression,Starts with the current character.(/^ n/i, which starts with n).

Note that we often refer to "\" as a "skip character". Used to escape some special symbols,Such as".","/"

Wildcards (lookarounds):Assert the existence of certain characters in some strings!

There are two types of lookarounds:lookaheads (forward lookup?=) and lookbehinds (reverse lookup?<=).

>Format:

Positive preview:(?=) The corresponding (? ;!) means negative

Reverse check:(?<=) The corresponding (?< ;!) means negative

Characters immediately before and after

$regex="/(?<= c) d (?= e) /";/* d followed by 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 is not immediately followed by 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:zero

Verify Zero Character Code

$regex="/he (?= l) lo/i";
$str="hello";
$matches=array ();
if (preg_match ($regex, $str, $matches)) {
var_dump ($matches);
}
echo "\ n";

No results printed!

$regex="/he (?= l) llo/i";
$str="hello";
$matches=array ();
if (preg_match ($regex, $str, $matches)) {
var_dump ($matches);
}
echo "\ n";

Can print the results!

Explanation:(?= l) means he is followed by an l character. But (?= l) does not occupy characters by itself,To distinguish from (l), (l) itself occupies one character.

Capture data

Grouping without specifying type,Will be fetched,For later use.

>indicates that the type refers to a wildcard.So only those parentheses without a question mark can be captured.

>References within the same expression are called back references.

>Calling format:\ number (such as \ 1).

$regex="/^ (chuanshanjia) [\ w \ s!] + \ 1 $/";
$matches=array ();
if (preg_match ($regex, $str, $matches)) {
var_dump ($matches);
}
echo "\ n";

>Avoid capturing data

Format:(?:pattern)

Pros:will keep the number of valid backreferences to a minimum,The code is more clear.

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

operation result

Lazy matching (remember:there are two operations,(See the principle section below)

Format:Qualifier?

Principle:"?":If preceded by a qualifier,The smallest data is used.For example, "*" will take 0, and "+" will take 1, and {3,5} will take 3.

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, using "+"

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

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

Result 4

Notes on regular expressions

Format:(?#comment content)

Purpose:mainly used for complex comments

Contribution code:is a regular expression for connecting to the mysql database

$regex="/
^ host=(?<! \.) ([\ d.] +) (?! \.) (?#host address)
\ |
([\ [email protected]#$%^&* () _ + \-] +) (?#username)
\ |
([\ [email protected]#$%^&* () _ + \-] +) (?#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

Special characters Explanation
* 0 to multiple times
+ 1 to multiple times can also be written as {1,}
? 0 or 1 time
. Matches all single characters except newlines
\ w [a-za-z0-9_]
\ s White space (space, newline, carriage return) [\ t \ n \ r]
\ d [0-9]

php
  • Previous Seventy-one of Manipulating Data in ASPNET 20: Protecting Connection Strings and Other Setting Information
  • Next A solution to the problem that the mysql database connection resources cannot be released in the CI framework