SoFunction
Updated on 2025-03-01

Introduction to commonly used regular expressions in PHP and application example code

Several tips for using PHP regular expressions
https:///article/

The following content is a common rule in PHP. Regular rule is a general syntax. You can download more comprehensive rules online to provide learning!
Function: segmentation, matching, search, replace

1. Two commonly used regular functions in php

preg_match(mode,string subject,array matches);

mode --- module, regular syntax
subject ---regular content
matches ---Result of regular

ereg(mode,string subject,array regs);

The above two functions return true or flase.

2. The elements contained in regular expressions

1. Atom (normal characters: a-z, A-Z, 0-9, atomic table, escape character)
2. Metacharacter (character with special functions)
3. Mode revision of political characters (the system built-in characters i, m, S, U...)

3. "Atom" in regular expressions

1. a-z A-Z_0-9 //The most common characters
2. (bfw)(sda) //Unit symbols included in parentheses, one bracket represents a whole
3. [sdwe][^mjnb] //The atomic table contained in square brackets. The atomic table represents exclusion or opposite content
Four, escape characters
\d Contains all numbers [0-9]
\D Except for all numbers [^0-9]
\w Contains all English characters [a-zA-Z_0-9]
\W Except for all English characters [^a-zA-Z_0-9] -----Match special characters
\s contains blank areas such as carriage return, line break, page, etc. [\f\n\r]

4. Regular expression metacharacter

* Match 0 or more times of the previous content
. Match 0 or more times of content, but does not include carriage return and line breaks
+ 1 or more times matched to the previous content
? Match 0 or 1 of the previous content
| Select a match, similar to the usage of || in php
^ Match the contents of the string header
$ Match the end of the string
\b Match word boundaries, the boundaries can be spaces or special symbols
\B Match unexpected content except word boundaries
{m} The number of repetitions matching the previous content is m times
{m,} The number of repetitions matching the previous content is greater than or equal to m
{m,n} The number of repetitions of the previous content m to n times
() Merge the overall match and put it in memory. You can use \\1\\2 to get the calls in turn

5. Pattern correction character in regular expressions
(1) Operation order
Follow the left-to-right operation rules

() The brackets are the highest first priority
* ? + {} Repeat match content is the second priority
^ $ \b Boundary processing is the third priority
| Conditional treatment is fourth
Finally, the match is calculated in the order of operations

(2) Mode modifier

It is a function that enhances and supplements for regular expressions, used outside of regular expressions

Example: /regular/U U represents a pattern correction character

The following are commonly used in PHP: (Note: case sensitive)
i Regular content is case-sensitive when matching (default is different)
m Use multiple lines to identify the match when matching the first or last content
s will escape carriage return cancellation is the unit matching such as.
x Ignore blanks in regular
A Forced match from scratch
D Force $ to match any content at the tail\n
U prohibits greed for mei matching, only tracks the most recent match character and ends, which are commonly used in regular expressions for collection programs.

example: