Home >Backend Development >PHP Tutorial >Summary of methods for matching characters with PHP regular expressions
This article introduces how to use regular expressions to match characters in PHP. If you need help, please refer to it.
1. Characters For a single character, it is usually expressed literally. The character indicates that the following character is a special character, so it is not interpreted literally, but is interpreted as a special character. For example, /b/ is equivalent to the character b. By adding a backslash in front of b, which is / /, the character becomes a special character, indicating the dividing line of matching a word. For several characters, it is usually stated that they are special, and the symbol indicates that the characters that follow are not special and should be interpreted literally 2.Character* Indicates that the matching character must be at the front. For example, the /^A/ character does not match the A in an A, but matches the first a in an A. 3. Character $ Similar to the ^ character, but matches the last character. For example, /t$/ does not match the t in the eater character, but matches the t in the eat character. 4.Character* Matches the character preceding the * character 0 or n times. For example, /bo*/ matches the boooo in A ghost booooed or the b in A bird warbled, but does not match any of the characters in A goat grunted. 5, character + Matches the character preceding the + character 1 or n times, equivalent to {1,}. For example, /a+/ matches a in the candy character and all a in the caaaaaaaaaaaady character. 6. Characters? Match? 0 or 1 times of the character preceding the character. For example, /e?le?/ matches el in the angel character and le in the angle character. 7. Character . (pay attention, it’s a dot) The . character matches any single character except newlines. For example, /.n/ matches nay, an apple is on the tree characters an and on, but does not match nay. 8, character (x) Match x characters and log the matching value. 9. Character x|y Match x or y. 10, characters {n} Here n is a positive integer, which means matching the previous n characters. For example, /a{2}/ does not match the a in "candy", but matches all a's in "caandy" and the first two a's in "caaaandy". 11. Character {n,} Here n is a positive integer, which means matching at least n previous characters. For example, /a{2}/ does not match the a in "candy", but matches all a's in "caandy" and all a's in "caaaandy". 12. Character {n,m} Here n and m are both positive integers, which means matching at least n and at most m previous characters. 13. Character [xyz] A list of characters that matches any character in the list. Character ranges can be indicated by hyphens -. For example, [abcd] and [a-d] are the same, they match b in brisket and c in echo. 14, character [^xyz] One-character complement, which matches everything except the listed characters. A range of characters can be indicated by a hyphen -. For example, [^abcd] and [^a-d] are equivalent. They match the r in brisket and the h in echo. 15, characters (space) Match a space 16, characters Matches a word boundary, such as a space. For example, /nw/ matches no in noonday, and /wy/ matches ly in "possible yesterday". Note: Specifies that the matching pattern must appear at one of the 2 boundaries at the beginning or end of the target character. 1 2 Next Page Last Page |