Some learning of PHP regular expressions

Source: Internet
Author: User

PHP is a regular expression of basic grammar application learning.



^ Start


$ end


* Match 0 or more of zero or more


"ab*" matches strings A and 0 or more B-composed strings ("a" "AB" "ABB" "abbbbbb" etc)


+ Match one or more single or more


The "ab+" matches the string A and one or more B strings ("AB" "ABB" "abbbbbb" etc) and the difference is that the following must match a B


? Match 0 or a zero or one


"AB" matches 0 or one B ("a", "AB")


.*? Used to match a large string of strings that do not require a regular string, that is, do not need to match directly to the past.


Example:


"a?b+$" matches a string ("B" "AB" "BBBBB" "abbbbbbb") ending with one or 0 a plus more than one B






Of course, you can add the number of restricted characters to the curly braces in the back.


"Ab{2}" matches one a must follow two B, "ABB"


"Ab{2,}" matches the B after a a must be greater than or equal to 2 "ABB" "ABBB" "ABBBBB"


"ab{2,5}" matches a b behind a a from 2 to 5 "ABB" "ABBB" "abbbb" "ABBBBB"




But less than two B would not be so "ab{,2}" must be This "ab{0,2}"


So for


* Just know the equivalent of {0}


+ equivalent to {1,}


? Equivalent to {0,1}




() is to link some strings together to match the


"A (BC) *" is matched by a and then followed by 0 BC or more BC "a" "abc" "ABCBC" "ABCBCBC" etc




| This character is equivalent to an OR operation


"Hi|hello" matches a string containing hi or hello


"(B|CD) EF" matches a string containing BEF or cdef


"(a|c) *d" match contains "D" "Ad" "CD" "AaB" "CCD" "Aaaaaaad" "CCCCCCCD"






. can represent all the single characters




"A.[0-9]" can match an A and then follow a string and then the last number "Aj9" "a<8" in which the middle of any single character is possible.


"^. {3}$ "ASD" starting and ending with three single characters will not be matched as "KJL" with extra three single characters.


[] The symbol includes live content that matches only one single character


"[AB]" is just a single or a single B equivalent to "a|b"


"[A-Z]" is a match for 26 lowercase letters


"^[a-za-z]" matches a string beginning with a letter


"[0-9]%" is the match of a string containing the form x percent


", [a-za-z0-9]$] matches a comma followed by a number or letter-ending string", 0 "", a "etc


PHP Some of the commonly used alternative symbols




# # or//indicates the delimiter single/represents escape character


\s means something that matches the white space.


\d matches a numeric character


\w matches any word character that includes an underscore



Online Big God summed up the comparison of the whole grammar.


Click to 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.