Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Regular expressions commonly used in PHP

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the knowledge of "regular expressions commonly used in PHP". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Two regular functions commonly used in 1.PHP

A.preg_match regular function, based on perl language

Syntax: preg_match (mode,string subject,array matches)

Description: mode parameter-regular module, that is, regular expression (syntax)

Subject parameter-regular content

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

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

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

two。 Elements included in regular expressions

a. Atoms (ordinary characters: amurz Amurz 0-9, atomic table, escape characters)

b. Metacharacters (characters with special functions, such as #, *)

c. Pattern modifier (the system has built-in characters I, m, S, U.)

3. "Atom" in regular expression

A.a-z Amurz _ 0-9 / / the most common character

B. (abc) (skd) / / Unit symbols enclosed in parentheses (as a whole)

C. [abcs] [^ abd] / / A list of atoms contained in square brackets, in which ^ represents exclusion or vice versa

d. Escape character

\ d contains all the numbers [0-9]

\ d except for all numbers [^ 0-9]

\ w contains all English characters [amurz Amurz 0-9]

\ W except for all English characters [^ amurz Amurz 0-9]

\ s enter, change lines, etc.

.

Note: parentheses must be a whole to match; square brackets can match as long as they are a subset of them (content exists)

4. Regular expression metacharacter

* match the previous content 0 times one or more times

. Match the content 0 times one or more times, but does not include enter feeds (on behalf of yourself, any content)

+ match one or more times of the previous content

? Match 0 or 1 times of the previous content

| | Select match similar to that in PHP | (because this operation matches with a weak type, which leads to the most overall match, similar to a word match) |

^ matches the first content of the string

$matches the end of the string

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

\ B match except unexpected content with word boundary (no word delimiter)

{m} the number of repetitions of the previous content is M

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

{mdirection n} matches the number of repetitions of the previous content from M to N

() merge the overall match and put it into memory, you can use\ 1\ 2 … Get (call what is put in memory) in turn

5. Operation sequence

Still follow the operation rules from left to → right

Priority:

() parentheses are highest because they are processed in memory.

*? + {} repeat matching content is the second

^ $\ b Boundary processing third

| | condition processing No. 4 |

Finally, the matching is calculated according to the order of operation.

6. Pattern modifier

Pattern modifiers are a feature that enhances and complements regular expressions and is used outside regular expressions such as: / regular / U

Common modifiers:

I regular content is case-insensitive when matching (default is case-sensitive)

M uses multi-line recognition matching when matching the first or last content.

S the escape carriage return is canceled for a single line match such as. When playing the Standard PvP match

X ignore whitespace in the rule

A forces a match from scratch

D forces $to match nothing at the tail\ n

U forbids greedy matching to track only the nearest match and ends, a regular expression commonly used in the collection program.

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 so that $matches [0] is the number of all pattern matches

Purpose: to intercept more detailed content, collect web pages, and analyze texts

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 regular expressions, similar to the str_replace string substitution learned before, but with better functionality than it.

Tip: 1. The replacement content can be a regular or an array regular.

2. The replacement content can be solved by the modifier e.

Purpose: replace some more complex content, can also be used in content conversion

9. Segmentation function

Preg_split regular cutting

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

Description: cut the relevant content through regular expressions, similar to the explode cutting function learned before, but explode can only cut in one way has limitations.

That's all for regular expressions commonly used in PHP. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report