Php regular expression sub-mode reverse reference learning Notes

Source: Internet
Author: User
Tags mixed php regular expression

You must use a regular expression to obtain the title of a string. The title label is h1 ~ H6.
Use a regular expression: "@

Example 1

 

The code is as follows: Copy code

<? Php
$ Str = 'Preg_match_all ("@

Print_r ($ all );
?>

Result
Array
(
[0] => Array
        (
[0] => [1] => [2] => [3] =>         )

[1] => Array
        (
[0] => 1
[1] => 2
[2] => 3
[3] => 4
        )

[2] => Array
        (
[0] => php php1
[1] => php php2
[2] => php php3
[3] => php php4
        )

)

Example 2

Mixed preg_replace (mixed pattern, mixed replacement, mixed subject [, int limit])

Function

Search for matches in pattern in subject and replace them with replacement. If limit is specified, only limit matches are replaced. If limit is omitted or its value is-1, all matches are replaced.
Replacement can contain reverse references in the \ n or $ n format. n can be 0 to 99, and \ n indicates text that matches the nth subpattern of pattern, \ 0 indicates the text that matches the entire pattern.

Sub-mode

The regular expression enclosed by parentheses in the $ pattern parameter. The number of child patterns is the number of left-to-right parentheses. (Pattern is the pattern)


Example 1: The simplest and most useful example is to determine the position of two identical words consecutively in the text.

The code is as follows: Copy code


<? Php
$ String = "Is the cost of gasoline going up ";
$ Pattern = "/\ B ([a-z] +) \ 1 \ B/I"; // \ 1 here cannot be used \ $1 or $1
$ Str = preg_replace ($ pattern, "\ 1", $ string); // Here \ 1 can be used \ $1 or $1 to reference the first child match
Echo $ str; // the result Is the cost of gasoline going up
?>

In this example, the subexpression is an item in parentheses. \ B matches the start or end of a word. + Repeat once or more times.
This subexpression matches one or more letter words, that is, '[a-z] +.

The second part of the regular expression is a reference to the previously captured sub-match, that is, the second occurrence word matched by the append expression, use '\ 1' to reference the first child Match. The First \ is an escape character.

I is the modifier in the regular expression. I: case insensitive.


Example 3:

The reverse reference ($0-99 or \ 0-99) and submode of the regular expression start.
$0 indicates the matching items of all matching modes. $1 is a 1st child match, and $2 to $99 are 2nd to 99th child matches in sequence.
If the character behind the submatch is a number, use curly brackets to separate it. Example: $ {1} 1.

The code is as follows: Copy code

<? Php
$ String = "{April 15,200 3 }";

// 'W' matches letters, numbers, and underscores, 'D' matches 0-99 numbers, and '+' matches multiple or more times.
$ Pattern = "/{(\ w +) (\ d +), (\ d +)}/I"; // match to be searched in the string
$ Replacement1 = "\$ {1} 1, \ $3 ";
$ Replacement2 = "\ $0 ";
$ Replacement3 = "\ $1 ";
$ Replacement4 = "\ $2 ";
$ Replacement5 = "\ $3 ";
Echo "<BR> ";

// The string is replaced with the text that matches the substring pattern in the nth captured parentheses
Print preg_replace ($ pattern, $ replacement1, $ string );
Echo "<BR> ";
Print preg_replace ($ pattern, $ replacement2, $ string );
Echo "<BR> ";
Print preg_replace ($ pattern, $ replacement3, $ string );
Echo "<BR> ";
Print preg_replace ($ pattern, $ replacement4, $ string );
Echo "<BR> ";
Print preg_replace ($ pattern, $ replacement5, $ string );
Echo "<BR> ";

// General expression
Print preg_replace ("/\ w +/I", "j", $ string );
?>

Effect:
April1, 2003
{10000l 15,200 3}
Else L
15
2003
{J, j}

The test has passed.

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.