Introduction and application of regular expressions commonly used in PHP example code _ Regular expression

Source: Internet
Author: User
Tags explode md5 mixed modifier php example php regular expression

A more comprehensive example, you can refer to
Most commonly used PHP regular expression collection and collation
Http://www.jb51.net/article/14049.htm

Summary of regular expressions in PHP
Http://www.jb51.net/article/19831.htm

Some tips for using PHP regular expressions
Http://www.jb51.net/article/19832.htm

The following is commonly used in PHP regular, is a common syntax, you can download a more comprehensive online, to provide learning!
Function: Split, match, find, replace

Two common regular functions in 1,php

Preg_match (mode,string subject,array matches);

Mode---module, regular syntax
Subject---Regular content
Matches---regular results

Ereg (mode,string subject,array regs);

The above two functions return True or flase.

2, the element that the regular expression contains

One, Atom (ordinary character: a-z,a-z,0-9, Atomic table, escape character)
Two, meta characters (characters with special functions)
Third, the mode of the revision character (System built-in part of the characters i,m,s,u ... )

3, the "atom" in the regular expression

One, A-Z a-z_0-9//most common characters
Two, (BFW) (SDA)//A cell symbol enclosed in parentheses, a bracket representing a whole
Three, [SDWE][^MJNB]///with square brackets included in the atomic table, the atomic table ^ represents the exclusion or the opposite content
Four, escape characters
\d contains all the numbers [0-9]
\d except for all numbers [^0-9]
\w contains all English characters [a-za-z_0-9]
\w [^a-za-z_0-9]-----Match Special characters except all English characters
\s contains blank areas such as carriage return, line wrapping, paging, etc. [\f\n\r]

4, regular expression meta-character

* 0 or more times to match the previous content
. Matches 0 or more occurrences of the content, but does not include carriage return line wrapping
+ 1 or more times to match the previous content
? Match 0 or 1 times before the previous content
| Select Match, similar to PHP | | Use of
^ matches the contents of a string header
$ Match String Tail content
\b Match word boundaries, boundaries can be spaces or special symbols
\b Matches unexpected content with word boundaries
{m} matches the previous content with a repeat number of M times
{m,} repeat times greater than or m times to match previous content
{M,n} matches the repeat number of the previous content m times to N times
() merges the whole match and puts it into memory, using \\1\\2 to get the call

5, pattern modifiers in regular expressions
(1) Order of operations
Follow left to right rules of operation

() parentheses are the highest first priority
* ? + {} Duplicate match content as second priority
^ $ \b Boundary processing for third priority
| Conditional treatment is four
Finally, the match is calculated in order of operation

(2) Pattern modifier

is a feature that is enhanced and supplemented for regular expressions and is used outside of the regular

Example:/Regular U/u denotes a pattern modifier

Some of the commonly used in PHP: (note: case-sensitive)
I regular content is not case-sensitive when matching (default is distinguished)
M uses multiple line recognition matching when matching first content or tail content
s the escape carriage return cancellation is a match for the unit.
x ignores whitespace in the regular
A force to start A match from scratch
D Force $ to match any contents of the tail \ n
U prohibit greedy Mei match, track only to a recent match and end, commonly used in the acquisition program of regular expressions

Example:

Copy Code code as follows:

<?php
$pot = "/\d{1,4} (. *) \d{1,2}\\1\d{1,2}/";
$cont = "2010-12-08";
if (Preg_match ($pot, $cont, $arr)) {
echo "Match succeeded". $arr [0];
}else{
echo "failed to match";
}
?>

6, Global match function

Preg_match_all (String pattern,string subject,array Matches[,int flags])

Main function: Intercept the more detailed content, collect the webpage, analyze the text
Example:
Copy Code code as follows:

<?php
$str = "Aaa<bbb>ccc<dddd>eee";
$mode = "/<.*>/u";

if (Preg_match_all ($mode, $str, $arr)) {
echo "Match succeeded". Print_r ($arr);
}else{
echo "failed to match";
}

?>

7, replace function
Preg_replace (mixed pattern,mixed replacement,mixed subject[,int limit])

More powerful than str_replace string substitution

Note: 1, the replacement content can be a regular or an array of regular
2, the replacement content can be solved by modifier e to replace the execution content

Use: Replace some of the more complex content, can also be used for the conversion of content
Example:
Copy Code code as follows:

<?php
$str = "Aaa<bbb>ccc<dddd>eee";
$mode = "/<.*>/u";
Echo preg_replace ($mode, "Replace content", $STR);
?>

Example 2: Array replacement
Copy Code code as follows:

<?php
$str = "Aaa<bbb>ccc<dddd>eee";
$mode =array ("/<bbb>/", "/<dddd>/");
$m =array ("/<yyy>/", "/<mmmm>/");

Echo preg_replace ($mode, $m, $STR);

?>
Example 3:MD5 Regular replacement
<?php
$STR = "Password 1:bbb\n password 2:bbb";
$mode = "/(BBB)/ie";

Parameter 1 is a regular replacement, such as over 2, which is also matched by an uppercase B in the following
Preg_replace ($mode, "MD5 (\\1)", $STR, 1);
Echo preg_replace ($mode, "MD5 (\\1)", $str);
?>


8, regular cutting function
Preg_split (String pattern,string subject[,int Limit[,int flags]])

Through regular expressions to cut related content, similar to the previous learned explode cutting function, but explode can only be cut in one way with limited

Example:
Copy Code code as follows:

<?php
$str = "Asd,sdsa,efsd,we." Dce,sec ";
$mode = "/[,.。 ]/";
$arr =preg_split ($mode, $STR);
for ($i =0; $i <count ($arr); $i + +) {
echo $arr [$i]. " <br> ";
}
?>

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.