Php regular expression quick start (1)

Source: Internet
Author: User
Tags ereg
Regular expressions are powerful tools for pattern matching and replacement. almost all programming languages support regular expression replacement.

Regular expressions are powerful tools for pattern matching and replacement. almost all programming languages support regular expression replacement.

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 ".

# P # php regular expression quick start (2) # e #

Php regular expression quick start (2)

Use instance

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

 

// The reference content is as follows: ereg (pattern, string) where pattern represents the regular expression pattern, while string represents the target object for performing the search and replacement operation. The verification email address is also used. 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 !"; }?>

 

② JavaScript 1.2 contains a powerful RegExp () object that 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 JavaScript to write the following script to verify that the email address entered by the user is valid.          

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.