Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use regular expressions in php

Shulou Source: shulou.com Published: 2022-06-01 06:51:27 10月01日 Update

Editor to share with you how to use regular expressions in php, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!

Atoms:

^ take the reverse in () or [] for example: [^\ d] equals matching non-numbers

Amurz Amurz Z0-9 / / the most common characters

(abc) / / those expanded in parentheses will be treated as a whole match, and the abc must be in one block at the same time in order to match successfully.

[abc] / / Square brackets are the opposite of parentheses, indicating that they can be a, b, c

\ d contains all numbers [0-9]

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

\ w contains all common characters [a-zA-Z_0-9]

\ w does not contain all common characters [^ a-zA-Z_0-9]

\ s contains blank areas, such as carriage return, line feeds, paging, etc. [\ f\ n\ r]

Metacharacters:

* match 0 times one or more times of the previous content for example: / key*rey/ matches y 0 times one or more times y * is a whole, regardless of whether there is y in the actual string; {"keyrey", "kerey": match successfully}

. Matches content 0 times one or more times, but does not include carriage return line feeds, wildcard characters; / key.*rey/ matches anything in the middle of key rey {keyfds*$7fd732f_) rey: match succeeded}

+ match one or more times of the previous content / key+rey/ matches one or more times of y. Y is required in the string to match successfully; {"keyrey", "keyyyyyrey": match success}, {"kerey": match failure}

? Match 0 times or 1 time of the previous content / key?rey/ 0 times or once of y; {"keyrey"kerey": match succeeded}, {"keyyrey": match failed}

| | Select match similar to PHP | | / keyrey | baidu/ {"keyrey%*baidu": match successfully}, {"fdas", "baidu": match success} |

^ matches the first part of the string / ^ keyrey/ must start with keyrey

The end of the matching string / keyrey$/ must end with $

\ b match word boundaries, which can be spaces or special characters mainly used to match spaces in Chinese context

\ B match content except with word boundaries

{m} match the previous content repeated to m times; / key {1} rey/ match y1 times {"keyrey": match succeeded}, {"keyyrey", "kerey": match failed}

{m,} match the repetition of the previous content m to n times; / key {1,} rey/ match 1 to n times {"keyrey", "keyyyyrey": success, "kerey": match failure}

The number of repetitions of the previous content is m to n / key {3 ~ 5} rey/ matches between 3 and 6 times.

() merge the overall match and put it into memory, you can use\ 1\ 2. Get / (gle) (. *)\\ 1.*\ 2 / {"gleAAAgle11AAA": successful}

Common pattern modifiers in PHP:

Operation priority: () *? + {} ^ $\ b | finally in the order of operation

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 cancels the escape carriage return to 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 know each other $matching tail does not have any content\ n

U forbids greedy matching to trace only to the nearest match and ends, and is often used in regular expressions on collection programs.

Non-greed in java (. *?)

The above is all the content of the article "how to use regular expressions in php". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Content success characters regular strings expressions common numbers whole time articles boundaries greed words tails frequently used times spaces white space operations Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Linux MySQL Huawei Xiaomi