POSIX Regular Expression for learning notes in PHP
1 Basic Knowledge
A regular expression is a method used to describe a text format. So far, the exact text we used previously) matching is also a regular expression. For example, we have previously searched Regular Expression terms, such as "shop" and "delivery ".
In PHP, matching a regular expression is a bit more like strstr () matching than equal comparison, because it is at a certain position in a string. If it is not specified, it may be at any position in the string) match another string. For example, the string "shop" matches the regular expression "shop ". It can also match regular expressions such as "h" and "ho.
In addition to exact matching characters, you can also use special characters to specify the meta-meaning of the expression ). For example, a special character can be used to specify a pattern that exists at the start or end of a string. A part of the pattern may be duplicated, or the pattern character belongs to a specific type. In addition, you can configure the match according to the appearance of special characters. Next, we will discuss these changes one by one.
2 character sets and Classes
Using character sets, you can immediately give regular expressions that are more powerful than exact matching. Character sets can be used to match any character of a specific type. In fact, they are wildcards.
First, you can use a character as a wildcard to replace any character other than the line break \ n. For example, a regular expression:
.
It can be matched with "cat", "sat", and "mat. Generally, this wildcard match is used for file name matching in the operating system.
However, you can use a regular expression to specify the type of the character to be matched and specify a set of characters. In the previous example, regular expressions match "cat" and "mat", but they can also match "# ". If you want to limit the character to a to z, you can specify it as follows:
[A-z]
Any content contained in square brackets []) is a character class-a combination of character sets to which matching characters belong. Note that the expression in square brackets only matches one character.
We can list a set, for example:
[Aeiou]
It can be used to represent the vowel.
You can also describe a range. Just as you used a hyphen before, it can also be a range set:
[A-zA-Z]
This range set represents any uppercase/lowercase letters.
In addition, a set can be used to indicate that the character does not belong to a specific set. For example:
[^ A-z]
It can be used to match any character not between a and z. When the delimiters ^) are included in square brackets, it indicates no. When this symbol is used outside square brackets, it represents another meaning. We will introduce it in detail later.
3. Repeated
In general, readers want to specify that a string or character class will appear more than once. You can use two special characters in a regular expression. The symbol "*" indicates that this mode can be repeated 0 times or more times. The symbol "+" indicates that this mode can be repeated once or more times. These two symbols should be placed behind the expression to be applied.
For example:
[[: Alnum:] +
It must contain at least one letter ".
4 subexpressions
Generally, it is useful to separate an expression into several subexpressions. For example, it can indicate "at least one of these strings needs to be exactly matched ". Parentheses can be used for implementation, just like the method in mathematical expressions.
For example:
(Very) * large
It can match with "large", "very large", and "very large.
5 subexpression count
You can use a numeric expression in braces {}) to specify the number of times the content can be repeated. You can specify an exact number of repeat ({3} indicates repeated 3 times) or a value of {2, 4} indicates repeated 2 ~ (4 times), or the repeating range of an open-source domain {2,} indicates that at least two times are repeated ).
For example:
(Very) {1, 3}
Match "very", "very", and "very ".
6. Locate the start or end of the string.
The [a-z] mode matches any string containing lowercase letters. It does not matter whether the string has only one character or contains only one matching character in the entire longer string.
You can also determine whether a specific subexpression appears at the start, end, or at both positions. It is useful when you want to determine that only the words to be searched are in the string and no other words appear.
Character delimiters ^) are used to start a regular expression, indicating that the substring must appear at the beginning of the string to be searched. The character "$" is used at the end of the regular expression, it indicates that the substring must appear at the end of the string.
For example, match bob at the beginning of the string:
^ Bob
This mode will match the string that com appears at the end of the string:
Com $
Finally, this pattern matches strings that only contain one character from a to z:
^ [A-z] $
7 Branch
You can use a vertical line in the regular expression to represent a choice. For example, to match com, edu, or net, you can use the following expression:
Com | edu | net
8 match special characters
If you want to match the special characters mentioned earlier in this section, such as., {, or $, you must add a backslash \) before them \). To match a backslash, you must use two backslash.
In PHP, the regular expression mode must be included in a single quotation mark string. Regular Expressions referenced by double quotation marks bring unnecessary complexity. PHP also uses a backslash to escape special characters, for example, a backslash.
If you want to match a backslash in the pattern, you must use two backslash to indicate that it is a backslash character rather than an escape character.
Similarly, for the same reason, if you want to use a backslash character in a PHP string referenced by a double quotation mark, you must use two backslashes. This can be confused. The result is that four backslashes are required for a PHP string containing a regular expression containing the backslash character. The PHP interpreter translates the four backslashes into two. Then, the regular expression interpreter resolves it into one.
The $ symbol is also a special character of the PHP string and regular expression referenced by double quotes. To make a $ character match in the mode, you must use "\ $ ". Because this string is referenced in double quotes, the PHP interpreter parses it into \ $, and the regular expression interpreter parses it into a $ character.
9. Application in smart forms
In a smart form application, regular expressions have at least two purposes. The first purpose is to find specific terms in customer feedback. The regular expression can be used to make it smarter. To use a string function that matches "shop", "customer service", or "retail", you must perform three different searches. If you use a regular expression, you can match all three at the same time, as shown below:
Shop | customer service | retail
The second purpose is to authenticate the user's email address in the program. This requires regular expressions to encode the standard format of the email address. The format contains numbers or punctuation marks, followed by the symbol "@", followed by a string consisting of text or numbers and characters, followed by a ". "point"), followed by a string consisting of text or numbers with a hyphen, there may be more periods until the end of the string, its encoding is as follows:
^ [A-zA-Z0-9 _ \-.] + @ [a-zA-Z0-9 \-] + \. [a-zA-Z0-9 \-.] + $
The subexpression ^ [a-zA-Z0-9 _ \-.] + represents a string starting with at least one letter, number, underline, hyphen, Dot, or combination of these characters ". Note that when you use a dot at the beginning or end of a character class, the dot will lose its special wildcard meaning and can only be a dot.
The symbol "@" matches the character "@".
The subexpression [a-zA-Z0-9 \-] + matches the host name that contains character numbers and hyphens. Note that we remove the hyphen because it is a special character in square brackets.
The character combination "\." matches. We use dot numbers outside the character class, so we must escape them so that they can match a dot character.
The subexpression [a-zA-Z0-9 \-\.] + $ matches the rest of the domain name, which contains letters, numbers, and hyphens, and can include more dots until the end of the string if needed.
It is not difficult to find that sometimes an invalid email address matches this regular expression. It is almost impossible to find all invalid emails. However, after analysis, the situation will be improved. You can refine this expression in many different ways. For example, you can list all valid top-level domains (TLD ). When limiting some objects, please be careful, because the validation function that may exclude 1% of valid data is more troublesome than the validation function that allows 10% of invalid data.
"I read a book every week, so I read 50 books a year." Come on... Don't give yourself too many reasons to give up.