Learning PHP regular expressions

Source: Internet
Author: User
Some PHP regular expression learning PHP regular expression basic syntax application learning.



^ Start


$ End


* Match zero or more


"AB *" matches strings a and 0 or more B ("a" "AB" abb "" abbbbbb "etc)


+ Match one or more


The difference between "AB +" matching string a and a string ("AB" abb "" abbbbbb "etc) consisting of one or more bits is that * must be followed by B.


? Matches zero or one


"AB? "Match 0 or a B (" a "," AB ")


.*? It is often used to match a large string, that is, a string that does not need to be matched, that is, to directly think about it.


Example:


"? B + $ "match one or zero a and add more than one string ending with B (" B "" AB "" bbbbb "" abbbbbbbbb ")






Of course, you can also add a limit on the number of characters in the braces.


"AB {2}" a must be followed by two B, that is, "abb"


"AB {2,}" B after matching a must be greater than or equal to two "abb" "abbb" "abbbbb"


"AB {}" matches B after a between 2 and 5 "abb" "abbb" "abbbb" "abbbbb"




But less than two bits cannot be like this. "AB {, 2}" must be like this. "AB }"


So


* It is equivalent to {0 ,}


+ Equivalent to {1 ,}


? Equivalent to {0, 1}




() Is to link some strings for matching


"A (bc) *" is to match a and then follow 0 bc or multiple bc "a" "abc" "abcbc" "abcbcbc" etc




| This character is equivalent to the OR operation.


"Hi | hello" matches strings that contain hi or hello


"(B | cd) ef" matches strings containing bef or cdef


"(A | c) * d" matches "d" ad "" cd "" aab "" ccd "" aaaaaaad "" cccccccccd"






. Can represent all single characters




"A. [0-9]" can match a and then end with a string with a number "aj9" "a <8". it can be any single character in the middle.


"^. {3} $" the "asd" that starts and ends with three single characters cannot be matched with any other three single characters, such as "kjl ."


[] This symbol contains only one character.


"[AB]" means that only matching a single a or a single B is equivalent to "a | B"


"[A-z]" matches 26 lower-case letters.


"^ [A-zA-Z]" matches a string starting with a letter


"[0-9] %" is to match a string containing x %.


", [A-zA-Z0-9] $" match a comma and then add a string ending with a number or letter ", 0" ", a" etc"


Some common replacement symbols in PHP




# Or // indicates the escape character for a single separator/


\ S indicates matching blank items


\ D matches a numeric character


\ W matches any word characters including underscores



The syntaxes summarized by the online experts are comprehensive.


Click Open link

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.