Home > Article > Backend Development > PHP Programming Tips: Learn Regular Expressions with Examples_PHP Tutorial
"^The" : Match the string starting with "The";
"of despair$": Match the string ending with "of despair"; "^abc$": Match the string starting with abc And the string ending with abc, actually only abc matches it; "notice": matches the string containing notice; You can see if you don't use what we mentioned The two characters (last example) mean that the pattern (regular expression) can appear anywhere in the string being tested, you don't lock it to both sides. There are several characters *, +, and ?, which are used to represent the number or order of occurrences of a character. They respectively represent: "zero or more", "one or more", and " zero or one." Here are some examples: "ab*": Matches a string consisting of a and 0 or more b ("a", "ab", "abbb", etc .); "ab+": Same as above, but with at least one b ("ab", "abbb", etc.); "ab?": Match 0 or a b; "a?b+$": Matches a string ending with one or zero a plus one or more b. You can also limit characters inside curly braces The number of occurrences, such as "ab{2}": matches an a followed by two b (no less) ("abb"); "ab{2, }": at least two b("abb", "abbbb", etc.); "ab{3,5}": 2-5 b("abbb", "abbbb", or "abbbbb"). You must also note that you must always specify (i.e., "{0,2}", not "{,2}"). Likewise, you must note, * , +, and ? are the same as the following three range annotations, "{0,}", "{1,}", and "{0,1}" respectively. Now put a certain number of characters into parentheses, for example: "a(bc)*": Match a followed by 0 or one "bc"; "a(bc){1,5}": one to 5 "bc." There is also one character │, equivalent to OR operation: "hi│hello" : Matches strings containing "hi" or "hello"; "(b│cd)ef": Matches strings containing "bef" or "cdef"; "( a│b)*c": Matches a string containing multiple (including 0) a or b, followed by a c string; A dot (.) can represent all single Characters: "a.[0-9]": an a followed by a character followed by a number (strings containing such a string will be matched, and this bracket will be omitted in the future) "^.{3}$": ends with three characters. The content enclosed in square brackets only matches a single character "[ab]": matches a single a or b ( and " a│b" is the same); "[a-d]": matches a single character from a to d (same as "a│b│c│d" and "[abcd]"); "^[a-zA-Z]": Matches strings starting with letters "[0-9]%": Matches strings containing x%http://www.bkjia.com/PHPjc/508679.html