In INTERNET programs, regular expressions are usually used to verify user input. After a user submits a FORM, it is not enough to determine whether the entered phone number, address, EMAIL address, and credit card number are valid.
What is a character cluster?
In INTERNET programs, regular expressions are usually used to verify user input. After a user submits a FORM, it is not enough to determine whether the entered phone number, address, EMAIL address, and credit card number are valid.
Therefore, we need to use a more free way to describe the mode we want. it is a character cluster. To create a character cluster that represents all vowel characters, put all the vowel characters in a square bracket:
[AaEeIiOoUu]
This mode matches any vowel character, but can only represent one character. The font size can be used to indicate the range of a character, for example:
[A-z] // match all lowercase letters
[A-Z] // match all uppercase letters
[A-zA-Z] // match all letters
[0-9] // match all numbers
[0-9 \. \-] // match all numbers, periods, and periods
[\ F \ r \ t \ n] // match all white characters
Similarly, these are only one character, which is very important. If you want to match a string consisting of a lowercase letter and a number, such as "z2", "t6", or "g7 ″, if it is not "ab2", "r2d3", or "b52", use this mode:
^ [A-z] [0-9] $
Although [a-z] represents the range of 26 letters, it can only match strings with lowercase letters with the first character.
^ Indicates the start of a string, but it has another meaning. When ^ is used in square brackets, it indicates "not" or "excluded", which is often used to remove a character. In the preceding example, the first character must not be a number:
^ [^ 0-9] [0-9] $
This mode matches "& 5", "g7", and "-2", but does not match "12" and "66. The following are examples of how to exclude specific characters:
[^ A-z] // All characters except lowercase letters
[^ \/\ ^] // All characters except (\) (/) (^)
[^ \ "\ '] // All characters except double quotation marks (") and single quotation marks (')
Special character "." (Point, period) is used to represent all characters except the "new line" in a regular expression. Therefore, the pattern "^. 5 $" matches any two-character string that ends with a number 5 and starts with another non-New Line character. Mode "." can match any string, except for empty strings and strings containing only one "new line.
PHP regular expressions have some built-in general character clusters. the list is as follows:
Character cluster meaning
[[: Alpha:] any letter
[[: Digit:] any number
[[: Alnum:] any letter or number
[[: Space:] any white characters
[[: Upper:] any uppercase letter
[[: Lower:] any lowercase letter
[[: Punct:] any punctuation marks
[[: Xdigit:] Any hexadecimal number, equivalent to [0-9a-fA-F]
The above is the details of the regular expression character cluster (1). For more information, see other related articles in the first PHP community!