Joke Collection Website - Public benefit messages - General rules for regularization of Thinkphp validator

General rules for regularization of Thinkphp validator

Regularity commonly used in ThinkPHP automatic verification

Generally speaking, we often set rules such as requirements and emails. In fact, these rules are also regular expression verification methods, but some commonly used regular expressions are defined in the system. The definition of these built-in regular expressions can refer to the regex method of the model class. General definitions of built-in support include:

The required fields must be e-mail address, url URL address, currency, number, postal code, integer, double floating point number and English letters, but not limited to these general rules. We can define it directly in the validation rules, so that we can use powerful regular expressions to validate form fields, such as:

Attach some regular expressions commonly used for form validation:

Match a specific string:

preliminary

Qualifiers are used to specify how many times a given component of a regular expression must appear to satisfy a match. Is there a * or+or? Or {n} or {n,} or {n, m}.

Qualifiers for regular expressions are:

The automatic verification mechanism of ThinkPHP is to verify the form data. Validation can support functions, callbacks, confirmations, equality, uniqueness, and regular expressions. What I'm going to talk about here is using regular expressions to verify.

special character

Many metacharacters need special treatment when matching. To match these special characters, you must first "escape" these characters, that is, put the backslash character \ before them. The following table lists the special characters in regular expressions:

Regular expressions describe a string matching pattern, which can be used to check whether a string contains a specific substring, replace a matched substring, or extract a substring that meets a specific condition from a string.

For example:

The method of constructing regular expressions is the same as that of creating mathematical expressions. That is, you can use various metacharacters and operators to combine small expressions to create larger expressions. The components of a regular expression can be a single character, a group of characters, a series of characters, a choice between characters or any combination of all these components.

A regular expression is a text pattern consisting of ordinary characters (such as characters A to Z) and special characters (called "metacharacters"). A pattern describes one or more strings to match when searching for text. As a template, regular expressions match character patterns with searched strings.

Ordinary characters include all printable and unprintable characters that are not explicitly designated as metacharacters. This includes all uppercase and lowercase letters, all numbers, all punctuation marks and some other symbols.

Non-printing characters can also be part of a regular expression. The following table lists the escape sequences that represent nonprinting characters:

The so-called special characters are characters with special meanings, such as the above-mentioned runoo*b, which simply represents the meaning of any string. If you want to find the * symbol in the string, you need to escape the *, that is, add a: runo*ob to match the runo ob in front of it.

Many metacharacters need special treatment when matching. To match these special characters, you must first "escape" these characters, that is, put the backslash character \ before them. The following table lists the special characters in regular expressions:

Qualifiers are used to specify how many times a given component of a regular expression must appear to satisfy a match. Is there a * or+or? Or {n} or {n,} or {n, m}.

Qualifiers for regular expressions are:

Since there may be more than 9 chapter numbers in a large input document, you need a method to handle two or three chapter numbers. Qualifiers give you this ability. The following regular expression matches any number of chapter titles:

Note that the qualifier appears after the range expression. Therefore, it applies to the entire expression range, in which case only numbers from 0 to 9 (inclusive) are specified.

The+qualifier is not used here because there is no need to have a number at or after the second position. No need? Characters, because using? Limit the chapter number to two digits. You need to match at least one number after the chapter and the space character.

If you know that the chapter number is limited to 99 chapters, you can use the following expression to specify at least one but at most two numbers.

The disadvantage of the above expression is that chapter numbers greater than 99 still only match the first two digits. Another disadvantage is that Chapter 0 will also match. A better expression that matches only two digits is as follows:

or

*,+qualifiers are greedy because they will match as many words as possible, adding only one? Non-greedy matching or minimum matching can be achieved.

For example, you can search an HTML document for chapter titles contained in the H 1 tag. The text in your document is as follows:

& ltH 1 Chapter 1-Introduction to Regular Expressions

Greed: The following expression matches less than the symbol (everything between:).

Non-greedy: If you only need to match the start and end H 1 tags, the following non-greedy expressions only match.

If you only want to match the starting H 1 tag, the expression is:

Through *,+or? After the qualifier? , the expression will be converted from a greedy expression to a non-greedy expression or a minimum match.

Locators enable you to fix regular expressions at the beginning or end of a line. They also enable you to create regular expressions that appear within words, at the beginning of words, or at the end of words.

The locators of regular expressions are: