PHP regular expression matching summary

Source: Internet
Author: User
PHP regular expression matching summary the PCRE pattern is a PHP regular expression matching rule.
It must be enclosed by delimiters (separators can be any non-alphanumeric, non-backslash, and non-blank characters), which means preg_macth ("[\ w {2, 3}]", $ subject) this type of regular match is also possible. Of course, the common type is/\ w {2, 3}/. when there are too many backslashes to escape, you can use other types of delimiters to close the package.
For example,/http: \/\ // can be written as @ http: //. This can also be matched with the name starting with http. After the separator ends, you can add a pattern modifier (the pattern modifier is actually an enhancement of the matching function. for example, if I is set, it will be case insensitive. m is used for multi-row Matching. s-point does not match the line break symbol)
Basic regular expression usage summary: (the most commonly seen matching)
\ Used to escape the backslash. it is required to match the backslash \\
[Abc] {} matches a string of 2 to 5 lengths consisting of any letter in abc.
{^ 123 $} m matches the row at the beginning and end of 123
Equivalent use of the three strings:
* = {0 ,}
+ = {1 ,}
? = {0, 1}
The following is an example of preg_replace ($ pattern, $ replace, $ subject) matching (in reality, regular expression matching is more complex than syntax)
1. group call for regular expression matching

 

2. remove unnecessary space regular match \ s to match a space character \ s + to match a space string +
$ Str = preg_replace ('/\ s +/', '', $ str );
3. use matched groups (you can enter various possible matching regular expressions in the regular array and use the latter one for replacement)
Preg_replace (array ('/\ d/', '/\ s/'), '*', $ str );
4 .? : The usage of a symbol does not match until a string ends.
Preg_replace ("/(? : <)(.*)(? :>)/I ", $ str, $ result );
Replace the content in <> with $ str at the same time.
Aaa Cdc matches bbb.

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.