Summary of regular expressions in PHP

Source: Internet
Author: User
Tags ereg explode mixed modifier regular expression

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: Mode parameter----regular module, i.e. regular expression (syntax)

Subject parameters----Regular content

Matches parameter----Regular result (get the form of an array)

B.ereg regular functions, POSIX based (Unix, Script)

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

  2. Elements included in regular expressions

A. Atom (ordinary character: A-Z-A-Z 0-9, Atomic table, escape character)

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

C. Pattern modifier (System built-in partial characters I, M, S, U ...)

  3. "Atoms" in regular expressions

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

B. (ABC) (SKD)//cell notation enclosed in parentheses (a whole)

C.[ABCS][^ABD]///with square brackets containing atomic tables, ^ in atomic tables excluding or opposing content

D. Escape characters

D contains all the numbers [0-9]

D except for all numbers [^0-9]

W contains all English characters [A-Z-Z 0-9]

W except for all English characters [^a-z-Z 0-9]

s return, line change, etc.

......

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

  4. Regular expression meta-characters

* 0 times 1 or more occurrences of the previous content

. Matches 0 times of content 1 or more times, but does not include carriage return line wrapping (refer to yourself, any content)

+ 1 or more times to match the previous content

? Match 0 or 1 times before the previous content

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

^ Match String header content

$ Match String Tail content

b matches the word boundary, the boundary can be a space or a special match (with Word delimiters, similar spaces)

B matches unexpected content except word boundary (no word delimiter)

{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

() Merge the overall match, and put in memory, you can use 1 2 ... Get (call into in-memory content) sequentially

  5. Order of operation

Still follow the rules of operation from left to right

Priority level:

() parentheses are highest because they are memory processed

* ? + {} Repeat match content second

^ $ b Border processing third

| Conditional Treatment IV

Finally, the match is calculated in order of operation

  6. Pattern 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 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 for a single line matching such as. Match the time

x ignores whitespace in the regular

A force to start A match from scratch

D force $ match tail without any content n

U prohibits greedy matches from tracking only to a recent match and ends, regular expressions commonly used on the acquisition program

  7. Matching function

Preg_match_all All matching functions

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

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

Uses: Intercepts the more detailed content, collects the webpage, analyzes the text

  8. Replacement function

Preg_replace Regular substitution 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

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

2, replacement content can be replaced by the modifier e to address the implementation of the content

Use: Replace some of the more complex content, can also 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]])

Note: Through regular expressions to cut related content, similar to the previous learned explode cutting function, but explode can only be cut in one way has limitations.

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.