What is regular expression matching in PHP? This is the use of PHP preg_match regular expressions. So what do we need to pay attention to for PHP preg_match regular expressions? Here we will introduce you in detail through the use of instances.
Use of PHP preg_match Regular Expressions:
With preg_match (), we can complete string rule matching. If a match is found, the preg_match () function returns 1; otherwise, 0. Another optional third parameter allows you to store the matched part in an array. This function can become very useful when verifying data. sun java Certification
- $ String="Football";
- If(Preg_match ('/Foo /',$ String)){
- // The matching is correct.
- }
The above example will be matched successfully because the word football contains foo. Now let's try a more complex one, such as verifying an Email address.
- $ String=First.last@domain.uno.dos";
- If(Preg_match (
- '/^ [^ 0-9] [a-zA-Z0-9 _] +
- ([.] [A-zA-Z0-9 _] +) * [@] [a-zA-Z0-9 _] +
- ([.] [A-zA-Z0-9 _] +) * [.] [a-zA-Z] {2, 4} $ /',
- $ String)){
- // Verify the Email address
- }
This example verifies that the Email address is in the correct format.
PHP preg_match regular expression rules:
Through the demonstration of the above example, we will understand the various rules represented by this regular expression.
PCRE, as its name implies, has the same syntax as the regular expression in Perl, so each regular expression must have a pair of delimiters. We generally use the separator.
The ^ at the beginning and $ at the end let PHP check from the beginning of the string to the end. If there is no $, the program will still match to the end of the Email.
◆ [And] are used to limit the license input type. For example, a-z allows all lowercase letters, A-Z allows all uppercase letters, 0-9 all numbers, and so on, and more other types.
◆ {And} are used to limit the expected number of characters. For example, {2, 4} indicates that each section of a string can contain 2-4 characters, such as .com.cn or. info. Here, "." is not a single character, because the allowed input type defined earlier than {2, 4} only contains uppercase and lowercase letters, so the segment only matches uppercase and lowercase letters.
◆ (And) is used to merge sections and define the characters that must exist in strings. (A | B | c) can match a, B, or c.
◆ (.) Will match all characters, while [.] will only match "." itself.
To use some symbols, you must add one in front. These characters are: () []. *? + ^ | $
The PHP preg_match regular expression is introduced here. I hope you can understand and master PHP preg_match regular expressions.