search
HomeBackend DevelopmentPHP TutorialDetailed explanation of sub-patterns of regular expressions in php_PHP tutorial

Detailed explanation of sub-patterns of regular expressions in php_PHP tutorial

Jul 13, 2016 am 10:56 AM
phpaboutarticlehavemodelregularofexpressionDetailed explanationneed

The article introduces a detailed explanation of the sub-patterns of regular expressions in PHP. Friends who need to know the sub-patterns of regular expressions in PHP can refer to it.

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

Function
Search subject for a match of pattern and replace with replacement. If limit is specified, only limit matches are replaced; if limit is omitted or has a value of -1, all matches are replaced.
Replacement can contain a reverse reference in the form of n or $n. n can be from 0 to 99. n represents the text matching the nth sub-pattern of pattern.

Sub-mode

The regular expression enclosed in parentheses in the $pattern parameter, the number of sub-patterns is the number of parentheses from left to right. (pattern is pattern)

First, let’s look at a piece of PHP code:

The code is as follows Copy code
 代码如下 复制代码

    $time = date ("Y-m-d H:i:s");
    $pattern = "/d{4}-d{2}-d{2} d{2}:d{2}:d{2}/i";
    if(preg_match($pattern,$time,$arr)){
    echo "
";<br>
    print_r($arr);        <br>
    echo "
";
    }
?>
$time = date ("Y-m-d H:i:s");

$pattern = "/d{4}-d{2}-d{2} d{2}:d{2}:d{2}/i";

If(preg_match($pattern,$time,$arr)){

echo "

";<br>
Print_r($arr); <br>
echo "
";
} ?>

Display results:

Array

(

[0] => 2012-06-23 03:08:45

) Have you noticed that the displayed result only has one piece of data, which is the time format that matches the matching pattern? So if there is only one record, why should it be saved in an array? Wouldn't it be better to save it directly as a string?

 代码如下 复制代码

    $time = date ("Y-m-d H:i:s");
    $pattern = "/(d{4})-(d{2})-(d{2}) (d{2}):(d{2}):(d{2})/i";
    if(preg_match($pattern,$time,$arr)){
    echo "
";<br>
    print_r($arr);        <br>
    echo "
";
    }
?>
With this question, let’s take a look at the sub-patterns in regular expressions. In regular expressions, you can use "(" and ")" to enclose substrings in a pattern to form a subpattern. When considered as a whole, the subpattern is equivalent to a single character. For example, we slightly modify the above code and change it to the following:
The code is as follows Copy code
$time = date ("Y-m-d H:i:s"); $pattern = "/(d{4})-(d{2})-(d{2}) (d{2}):(d{2}):(d{2})/i"; If(preg_match($pattern,$time,$arr)){ echo "
";
Print_r($arr); 
echo "
"; } ?>

Note: I only modified $pattern. In the matching pattern, I used brackets ()

Execution result:


Array
(
[0] => 2012-06-23 03:19:23
[1] => 2012
[2] => 06
[3] => 23
[4] => 03
[5] => 19
[6] => 23
)
Summary: We can use parentheses to group the entire matching pattern. By default, each group will automatically have a group number. The rule is, from left to right, with the left parenthesis of the group as the mark, the first group that appears The first one is group number 1, the second one is group number 2, and so on. Among them, group 0 corresponds to the entire regular expression. After the entire regular matching pattern is grouped, you can further use "backward reference" to repeatedly search for text matching a previous group. For example: 1 represents the text matched by group 1, 2 represents the text matched by group 2, etc. We can further modify the code as follows:

The code is as follows Copy code
 代码如下 复制代码
    $time = date ("Y-m-d H:i:s");
    $pattern = "/(d{4})-(d{2})-(d{2}) (d{2}):(d{2}):(d{2})/i";
    $replacement = "$time格式为:
替换后的格式为:1年2月3日 4时5分6秒";
    print preg_replace($pattern, $replacement, $time);
    if(preg_match($pattern,$time,$arr)){
        echo "
";<br>
        print_r($arr);        <br>
        echo "
";
    }
?>
$time = date ("Y-m-d H:i:s"); $pattern = "/(d{4})-(d{2})-(d{2}) (d{2}):(d{2}):(d{2})/i"; $replacement = "The format of $time is: $0
The replaced format is: February 3, 1 year, 4 hours, 5 minutes and 6 seconds"; Print preg_replace($pattern, $replacement, $time); If(preg_match($pattern,$time,$arr)){ echo "
";
              print_r($arr);                                            
echo "
"; } ?>

Note:

Because it is in double quotes, you should use two backslashes when using grouping, such as: 1, and if it is in single quotes, use one backslash, such as: 1
1 is used to capture the content of group one: 2012, 6 is used to capture the content of group 6     
Execution result:


The format of $time is: 2012-06-23 03:30:31
The replaced format is: June 23, 2012 03:30:31
Array
(
[0] => 2012-06-23 03:30:31
[1] => 2012
[2] => 06
[3] => 23
[4] => 03
[5] => 30
[6] => 31
)

Advanced Regular Expressions

In addition to POSIX BRE and ERE, libutilitis also supports advanced regular expression languages ​​compatible with TCL 8.2
Law (ARE). ARE mode can be enabled by adding the prefix "***:" to the stRegEx parameter. This prefix replaces
Cover bExtended option. Basically, ARE is a superset of ERE. It performs the following steps based on ERE
Item extension:

1. Support "lazy matching" (also called "non-greedy matching" or "shortest matching"): in '?', '*', '+' or '{m,n}'
You can enable shortest matching by appending the '?' symbol after , so that the regular expression clause matches
if the conditions are met. Match as few characters as possible (the default is to match as many characters as possible). For example: apply "a.*b" to "abab"
When "a.*?b" is used, it will match the entire string ("abab"). If "a.*?b" is used, it will only match the first two characters ("ab").

2. Supports forward reference matching of subexpressions: In stRegEx, you can use 'n' to forward reference a previously defined
Subexpression. For example: "(a.*)1" can match "abcabc" etc.

3. Unnamed subexpression: Use "(?:expression)" to create an unnamed expression. The unnamed expression does not return
to an 'n' match.

4. Forward prediction: To hit a match, the specified conditions must be met forward. Forward prediction is divided into positive prediction and negative prediction
There are two kinds. The syntax for positive prediction is: "(?=expression)", for example: "bai.*(?=yang)" matches "bai yang"
The first four characters ("bai ") in , but when matching, ensure that the string must contain "yang".
after "bai.*" The syntax for negative judgment is: "(?!expression)", for example: "bai.*(?!yang)" matches the first
of "bai shan" Four characters, but when matching, it is ensured that "yang" does not appear after "bai.*" in the string.

5. Support mode switching prefix, "***:" can be followed by a pattern string in the form of "(?pattern string)", pattern
The string affects the semantics and behavior of subsequent expressions. The pattern string can be a combination of the following characters:

b - Switch to POSIX BRE mode, overriding the bExtended option.
e - Switch to POSIX ERE mode, overriding the bExtended option.
​​ q - Switch to text literal matching mode, the characters in the expression are searched as text, and all regular expressions are canceled
Semantics. This mode reduces regular matching to a simple string search. "***=" prefix is ​​its shortcut representation
Method, meaning: "***=" is equivalent to "***:(?q)".

c - Perform case-sensitive matching, overrides the bNoCase option.
i - performs a case-ignoring match, overrides the bNoCase option.

n - Enable line-sensitive matching: '^' and '$' match the beginning and end of the line; '.' and negation sets ('[^...]') do not
Matches newline characters. This functionality is equivalent to the 'pw' pattern string. Override the bNewLine option.
m - Same as 'n'.
p - '^' and '$' only match the beginning and end of the entire string, not lines; '.' and the negative set do not match newlines.
               Overrides the bNewLine option.
w - '^' and '$' match the beginning and end of the line; '.' and the negative set match newlines. Override the bNewLine option.
s - '^' and '$' only match the beginning and end of the entire string, not lines; '.' and the negative set match newlines. Reply
                              Cover bNewLine option. This mode is used by default in ARE state.

x - Turn on extended mode: In extended mode, whitespace characters and content after the comment character '#' in the expression will be ignored
For example:
           @code@
(?x)
s+ ([[:graph:]]+) # first number
s+ ([[:graph:]]+) # second number
           @code@
Equivalent to "s+([[:graph:]]+)s+([[:graph:]]+)".
T -Close the extension mode without ignoring the content of the blank and notes. This mode is used by default in ARE state.

6. Perl-style character class escape sequence different from BRE/ERE mode:

perl class Equivalent POSIX expression Description
-------------------------------------------------- --------------------------
a - Bell character
A - No matter what the current mode is, only the beginning of the entire string is matched
b - Backspace character ('x08')
B - The escape character itself ('')
cX - Control symbol-X (= X & 037)
D [[: Digit:]] 10 -in -made numbers ('0' -'9')
D        [^[:digit:]]                                                                                       e - Exit character ('x1B')
f - Form feed ('x0C')
m [[:<: word starting position> M       [[:>:]]                                                                                                                                                                                                                                                            Since n - Newline character ('x0A')
r - Carriage return character ('x0D')
s        [[:space:]]                                                                                                                                         S        [^[:space:]]                                                                                                                                                                 t                                                                                                                                                      Tab character ('x09') 
uX - 16-bit UNICODE characters (X∈[0000 .. FFFF])
UX - 32-bit UNICODE characters (X∈[00000000 .. FFFFFFFF])
v - Vertical tab character ('x0B')
w [[:alnum:]_] Characters that make up words
W [^[:alnum:]_] Non-word characters
xX - 8-bit characters (X∈[00 .. FF])
y - Word boundary (m or M)
Y - Non-word boundary
Z - No matter what the current mode is, only the last
of the entire string is matched. - NULL, empty character
X - Subexpression forward reference (X∈[1 .. 9])
XX - Subexpression forward reference or 8-character octal representation
XXX - Subexpression forward reference or 8-character octal representation

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632203.htmlTechArticleThe article introduces a detailed explanation of the sub-patterns of regular expressions in PHP. If you need to know the sub-patterns of regular expressions in PHP Friends who know the pattern can refer to it. Function mixed preg_replace ( mixed pattern,...
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use