PHP regular expression high-speed learning method

Source: Internet
Author: User
Tags ereg php regular expression
Quick learning method for PHP Regular Expressions 1. Getting started simple introduction, regular expressions are a powerful tool for pattern matching and replacement. We can find regular expressions in almost all UNIX-based tools, such as vi editor, Perl or PHP script language, and quick learning of awk or sedshel PHP regular expressions.
1. Getting started

In short, regular expressions are a powerful tool for pattern matching and replacement. We can find regular expressions in almost all UNIX-based tools, such as the vi editor, Perl or PHP scripting language, and awk or sed shell programs. In addition, client scripting languages such as java script also provide support for regular expressions. It can be seen that regular expressions have gone beyond the limits of a language or system and become widely accepted concepts and functions.
Regular expressions allow users to construct matching modes by using a series of special characters, and then compare the matching modes with target objects such as data files, program input, and form input on the web page, execute the corresponding program based on whether the comparison object contains the matching mode.
For example, the most common application of regular expressions is to verify whether the format of the email address entered by the user online is correct. If the regular expression is used to verify that the email address format is correct, the form information entered by the user will be processed normally. Otherwise, if the email address entered by the user does not match the regular expression mode, A prompt will pop up asking the user to re-enter the correct email address. It can be seen that regular expressions play an important role in the logic judgment of WEB applications.

2. Basic syntax

After a preliminary understanding of the functions and functions of a regular expression, let's take a look at the syntax format of the regular expression.
The regular expression format is generally as follows:
/Love/
The part between the "/" delimiters is the pattern to be matched in the target object. You only need to place the pattern content of the desired matching object between the "/" delimiters. To enable users to customize the Mode content more flexibly, regular expressions provide special "metacharacters ". Metacharacters are special characters that have special meanings in regular expressions. they can be used to specify the mode in which the leading character (that is, the character located before the metacharacters) appears in the target object.
Frequently used metacharacters include "+", "*", and "?". The "+" metacharacter specifies that its leading character must appear one or more times consecutively in the target object, the "*" metacharacter specifies that the leading character must appear zero or multiple times in the target object, and "?" Metacharacter specifies that the leading object must appear zero or once consecutively in the target object.
Next, let's take a look at the specific application of the regular expression metacharacters.
/Fo +/
Because the above regular expression contains the "+" metacharacter, it can be used with the "fool", "fo ", or "football", and so on, one or more character strings that match the letter "f" consecutively.
/Eg */
Because the above regular expression contains the "*" metacharacter, it can be used with the "easy", "ego ", or, "egg" and other strings that appear after the letter e are matched with zero or multiple letter g consecutively.
/Wil? /
Because the above regular expression contains "?" Metacharacter, indicating that it can match the "Win" or "Wilson" in the target object, and matches zero or one character string after the letter I.
In addition to metacharacters, you can also precisely specify the frequency of occurrence of a pattern in a matching object. For example,
/Jim {2, 6 }/
The above regular expression specifies that the character m can appear 2-6 times consecutively in the matching object. Therefore, the above regular expression can match strings such as jimmy or jimmm.pdf.
After a preliminary understanding of how to use regular expressions, let's take a look at the usage of several other important metacharacters.
\ S: used to match a single space character, including the tab key and line break;
\ S: used to match all characters except a single space character;
\ D: used to match numbers from 0 to 9;
\ W: used to match letters, numbers, or underscores;
\ W: used to match all characters that do not match \ w;
.: Used to match all characters except line breaks.
(Note: \ s and \ S and \ w and \ W can be regarded as inverse operations)
Next, let's take a look at how to use the above metacharacters in regular expressions through examples.
/\ S +/
The above regular expression can be used to match one or more space characters in the target object.
/\ D000/
If we have a complex financial statement in our hands, we can easily find all the payments totaling thousands of yuan using the regular expression above.

In addition to the metacharacters described above, regular expressions also have a unique special character, that is, the positioning character. Specifies the position where the matching mode appears in the target object.
Commonly used positioning characters include "^", "$", "\ B", and "\ B ". The "^" operator specifies that the matching mode must start with the target string, and the "$" operator specifies that the matching mode must end with the target object, the \ B locator specifies that the matching mode must appear at either the beginning or end of the target string, the "\ B" locator specifies that the matched object must be within the boundary of the start and end of the target string. that is, the matched object cannot start with the target string, it cannot end with the target string. Similarly, we can regard "^" and "$" as well as "\ B" and "\ B" as two sets of operators for inverse operation. For example:
/^ Hell/
Because the above regular expression contains the "^" operator, it can match a string starting with "hell", "hello", or "hellhound" in the target object.
/Ar $/
Because the above regular expression contains the "$" operator, it can match the string ending with "car", "bar", or "ar" in the target object.

/\ Bbom/
Because the above regular expression pattern starts with "\ B", it can match strings starting with "bomb" or "bom" in the target object.
/Man \ B/
Because the above regular expression pattern ends with the "\ B" operator, it can match the string ending with "human", "woman", or "man" in the target object.
To make it easier for users to set matching modes flexibly, regular expressions allow users to specify a range in the matching mode, not limited to specific characters. For example:
/[A-Z]/
The above regular expression will match any uppercase letter from A to Z.
/[A-z]/
The above regular expression will match any lowercase letter from a to z.
/[0-9]/
The above regular expression will match any number from 0 to 9.
/([A-z] [A-Z] [0-9]) +/
The above regular expression will match any string consisting of letters and numbers, such as "aB0. Note that you can use "()" in a regular expression to combine strings. The content contained by the "()" symbol must appear in the target object at the same time. Therefore, the above regular expression cannot match strings such as "abc", because the last character in "abc" is a letter rather than a number.
If we want to implement the "or" operation similar to the programming logic in the regular expression, and select one of multiple different modes for matching, we can use the pipe character "| ". For example:
/To | too | 2/
The above regular expression will match "to", "too", or "2" in the target object.
There is also a common operator in the regular expression, that is, the negative character "[^]". Unlike the positioning character "^" described above, the "[^]" negation specifies that the target object cannot contain strings specified in the pattern. For example:
/[^ A-C]/
The above string will match any character except A, B, and C in the target object. In general, when "^" appears in "[]", it is regarded as a negative operator. when "^" is located outside of "[]" or, it should be regarded as a positioning character.
Finally, you can use the escape character "\" to add metacharacters to the regular expression mode and find matching objects. For example:
/Th \*/
The above regular expression will match "Th *" in The target object rather than ".

3. use instances

① In PHP, the ereg () function can be used for pattern matching. The format of the ereg () function is as follows:
 

Reference content is as follows:
Ereg (pattern, string)
Here, pattern indicates the regular expression mode, while string indicates the target object for performing the search and replacement operation. Verify the email address. the code written in PHP is as follows:

<? Php
If (ereg ('^ ([a-zA-Z0-9 _-]) + @ ([a-zA-Z0-9 _-]) + (\. [a-zA-Z0-9 _-]) + ", $ email )){
Echo "Your email address is correct !";}
Else {
Echo "Please try again !";
}
?>


② Java script 1.2 contains a powerful RegExp () object, which can be used for matching regular expressions. The test () method can check whether the target object contains the matching mode and return true or false accordingly.
We can use java script to write the following script to verify the validity of the email address entered by the user.


Reference content is as follows:
 
  
   

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.