PHP Regular Expression Summary _php tutorial

Source: Internet
Author: User
Tags ereg php regular expression

Summary of regular expressions in PHP


  Two common regular functions in 1.PHP

A.preg_match regular functions, based on the Perl language

Syntax: Preg_match (mode,string Subject,array matches)

Description: The mode parameter----regular module, which is the regular expression (syntax)

The contents of the subject parameter----regular

Matches parameter----The result of a regular (get the form of an array)

B.ereg regular function to POSIX basis (Unix, Script)

Syntax: Ereg (mode, string subject, array regs)

  2. Elements included in a regular expression

A. Atoms (ordinary characters: A-Z-A-0-9, atomic tables, escape characters)

B. metacharacters (characters with special features, such as: #, *)

C. Pattern modifier (System built-in part character I, M, S, U ...)

  3. "Atoms" in regular expressions

A.a-z A-Z _ 0-9//most common characters

B. (ABC) (SKD)//cell symbol enclosed in parentheses (one whole)

C.[ABCS][^ABD]//The atomic table enclosed in square brackets, the ^ in the atomic table represents the excluded or opposite content

D. Escape character

\d contains all the numbers [0-9]

\d except all numbers [^0-9]

\w includes all English characters [A-Z A-Z 0-9]

\w except for all English characters [^a-z A-Z 0-9]

\s return, line change, etc.

......

Note: Parentheses must be the whole to match; Square brackets can match as long as they are subsets (content exists)

  4. Regular expression meta-characters

* Match the previous content 0 times 1 or more times

. Matches content 0 times 1 times or more, but does not include carriage returns (substituting for yourself, anything)

+ match 1 or more times of previous content

? Match the previous content 0 or 1 times

| Select match like in PHP | (because this operation conforms to the weak type leading to the most overall match, similar to a word match)

^ Match String header contents

$ match String Trailing content

\b Matches a word boundary, which can be either a space or a special match (with a word delimiter, like a space)

\b Match exception with word boundary unexpected content (no word delimiter)

{m} matches the previous content with a repeat number of M times

{m,} The number of repetitions of the previous content is greater than or equal to M times

{M,n} matches the number of repetitions of the previous content m times to N times

() merge the whole match, and put in memory, can use \\1 \\2 ... Get (call to put in-memory content)

  5. Sequence of operations

Still follow the arithmetic rules from left to right

Priority level:

() parentheses because it is memory processing, so the highest

* ? + {} Repeat match content next

^ $ \b Border Processing third

| Condition Processing IV

Finally, the matching is calculated in order of operation

  6. Mode modifier

A pattern modifier is a feature that is enhanced and supplemented for regular expressions and is used outside of the regular for example:/Regular/U

Common modifiers:

I regular content is not case-sensitive at match time (by default it is differentiated)

M uses multi-line recognition matching when matching first content or tail content

S will escape carriage return cancellation is for a single line match as. Match the time

x ignores whitespace in the regular

A Force A match from the beginning

D force $ match tail without any content \ n

U disallow greedy match to trace only to the nearest match and end, regular expression commonly used on the collector

  7. Matching function

Preg_match_all All matching functions

Syntax: Preg_match_all (string pattern, string subject, array matches [, int flags])

Description: Sort the results to make $matches [0] match the number of all patterns

Purpose: Intercept more detailed content, collect Web pages, analyze text

  8. Replacement function

Preg_replace Regular Replacement function

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

Description: Replace the relevant content with a regular expression, similar to the previously learned str_replace string substitution, but the function is stronger than it

Hint: 1, the replacement content can be a regular or an array of regular

2, the replacement content can be replaced by the modifier e to resolve the content of the execution

Purpose: Replace some of the more complex content, also can be used for the conversion of content

  9. Split function

Preg_split Regular Cutting

Syntax: Preg_split (string pattern, string subject [, int limit [, int flags]])

Description: Cutting related content with regular expressions, similar to the explode cutting function previously learned, but explode can only be cut in one way with limitations.

http://www.bkjia.com/PHPjc/957532.html www.bkjia.com true http://www.bkjia.com/PHPjc/957532.html techarticle PHP Regular Expression Summary 1.PHP two commonly used regular function A.preg_match regular function, the Perl language as the base syntax: Preg_match (mode,string subject,array matches) Description: Mo ...

  • 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.