Review
Regular expressions are a powerful tool that can be used for pattern matching and substitution. Regular expressions allow the user to build a matching pattern by using a series of extraordinary characters, then compare the matching pattern with the target objects such as data file, program input, and form input of the Web page, and execute the corresponding program according to whether the matching pattern is included in the comparison object.
How do I use basic pattern matching?
Patterns, the most basic element of regular expressions, are a set of characters that describe the character of a string. Patterns can be very simple, composed of ordinary strings, can also be very complex, often with extraordinary characters to represent a range of characters repeatedly appear, or to represent the context. Let's look at the extraordinary characters in some regular expressions.
The extraordinary character "^" is used to match a string that begins with the specified string. For example:
"^hello": This pattern and the string "hello,php world!" Match, but does not match "Say hello to".
The extraordinary character "$" is used to match a string ending with the specified string. For example:
"you$": This mode and "How are You" match, and "your" does not match.
When the extraordinary characters "^" and "$" are used at the same time, they represent exact matches. For example:
"^hello$": This pattern only matches the string "Hello".
If a pattern does not include "^" and "$", it matches any string that contains the pattern. Example: "You": With the string "What is your name?" is a match.
The letters in the pattern are just plain characters, and the numbers are the same.
If you want to use some other slightly more complex characters, such as punctuation and whitespace characters (such as spaces, tabs, etc.), you need to use an escape sequence. All escape sequences begin with a backslash (""), such as a tab's escape sequence: "T". So if we're going to detect whether a string starts with tabs, you can use this pattern:
"^t"
Similarly, "n" means a newline, "R" is a carriage return, and the backslash itself is "", period. With "." Represents, and so forth.
How do I use a character cluster?
If you want to determine whether the user's phone number, address, email address, credit card number, etc. are valid, it is not enough to use a normal literal string comparison. So in a better way to describe the pattern we want, this is the character cluster.
For example, to create a cluster of characters that represents all vowel characters, you can do this:
[Aaeeiioouu]: This pattern matches any vowel character, but can only represent one character.
With the extraordinary symbol "-" can represent a range of characters, such as:
"[A-Z]"//match letter A-Z, that is, all lowercase letters
"[A-Z]"//match letter A-Z, that is, all uppercase letters
"[A-za-z]"//Match all the letters
[0-9] "//Match all numbers
"[0-9.-]"//Match all numbers, and periods and minus signs
"[]"///Match all white characters
Again, these will only match one character.
If you want to match a string composed of a lowercase and a digit, such as "A4", "B5" or "F1", but not "aa4", "b5a4" or "F12", use this pattern:
"^[a-z][0-9]$"
Although [A-z] represents a 26-letter range, it can only match a string with the first character being a lowercase letter.
We already know that "^" represents the beginning of a string, but when using "^" in a set of square brackets, it means "non" or "exclude", which is often used to weed out a character.