Using regular Expressions (a) _php tutorial

Source: Internet
Author: User
Tags uppercase letter
If we ask the enthusiasts of UNIX systems what they like best, the answer is that in addition to a stable system and the ability to remotely launch, nine out of ten people will refer to regular expressions, and if we ask them what their biggest headache is, they might be regular expressions in addition to the complex process control and installation process. So what exactly is a regular expression? How can you really master regular expressions and use them correctly? This article will be introduced to help readers who are eager to understand and master regular expressions.

Introduction to Getting Started

Simply put, regular expressions are a powerful tool that can be used for pattern matching and substitution. We can find regular expressions in almost all UNIX-based system tools, such as VI Editor, perl or PHP scripting language, and awk or sed shell programs. In addition, the scripting language of the client, such as javascript/"target=" _blank ">javascript, also provides support for regular expressions. It can be seen that the regular expression has gone beyond the limits of a language or a system, becoming a widely accepted concept and function.

Regular expressions allow the user to construct a matching pattern by using a series of special characters, and then compare the matching pattern with the data file, the program input, the form input of the Web page, and the corresponding program according to whether the matching pattern is included in the comparison object.

For example, one of the most common uses of regular expressions is to verify that the e-mail addresses that users enter online are in the correct format. If the user's e-mail address is properly formatted with a regular expression, the form information that the user fills in will be processed normally, and if the user enters a message address that does not match the regular expression pattern, a prompt will be displayed asking the user to reenter the correct email address. This shows that the regular expression plays an important role in the logic judgment of Web application.

Basic syntax

After a preliminary understanding of the function and function of regular expressions, let's take a specific look at the syntax format of the regular expression.

Regular expressions are generally in the following form:

/love/

The part where the "/" delimiter is located is the pattern that will be matched in the target object. The user simply puts the pattern content that you want to find the matching object in between the "/" delimiter. To enable the user to customize the schema content more flexibly, the regular expression provides a special "meta-character". A meta-character is a special character in a regular expression that can be used to specify its leading character (that is, the character in front of the metacharacters) in the target object.

The more commonly used metacharacters are: "+", "*", and "?". where the "+" metacharacters stipulate that their leading characters must be in the target object? continues to occur one or more times, the "*" metacharacters stipulate that the leading character must appear 0 or more times in the target object, and "?" A meta-character specifies that its leading object must appear 0 or more times in the target object.

Below, let's look at the specific application of the regular expression meta-character.

/fo+/

Because the above regular expression contains the "+" metacharacters, it can be matched with a string of "fool", "fo", or "football" in the target object, and so on with one or more letters O consecutively after the letter F.

/eg*/

Because the above regular expression contains the "*" meta-character, it can be matched with a string such as "Easy", "ego", or "egg" in the target object, such as 0 or more occurrences of the letter G after the letter E.

/wil?/

Because the above regular expression contains the "? A meta-character that represents a string that can match "Win" in the target object, or "Wilson", and so on, after the letter I, 0 consecutive or one letter L.

In addition to metacharacters, users can specify exactly how often the pattern appears in the matching object. For example

/jim{2,6}/

The regular expression above specifies that the character m can appear continuously 2-6 times in the matching object, so the above regular expression can match the string such as Jimmy or Jimmmmmy.

After a preliminary understanding of how to use regular expressions, let's take a look at some of the other important metacharacters uses.

S: Used to match a single space character, including 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 underscore characters;

W: used to match all characters that do not match w;

. : Used to match all characters except the line break.

(Note: We can think of S and S and W and W as inverse)

Below, we'll look at how to use the above metacharacters in regular expressions using an example.

/s+/

The preceding 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 hand, we can easily find all sums amounting to thousands of dollars through these regular expressions.

In addition to the meta-characters we have described above, there is another unique special character in the regular expression, the locator. The locator is used to specify where the matching pattern appears in the target object.

The more commonly used locators include: "^", "$", "" "and"B". Where the 癪 "locator specifies that the matching pattern must be at the beginning of the target string, the" $ "locator specifies that the matching pattern must be at the end of the target object, and that the match pattern must be one of the two boundaries at the beginning or end of the target string, and" B " A locator specifies that the matching object must be within the first and end two boundaries of the target string, that is, the matching object cannot be either the beginning of the target string or the end of the target string. Similarly, we can think of "^" and "$" and "" "and" B "as two sets of locators for reciprocal operations. For example:

/^hell/

Because the preceding regular expression contains a "^" Locator, you can match a string that begins with "Hell", "Hello" or "Hellhound" in the target object.

/ar$/

Because the above regular expression contains a "$" locator, you can match a string that ends with "car", "bar" or "AR" in the target object.

/bom/

Because the regular expression pattern above starts with a locator, you can match a string that starts with "bomb" or "BOM" in the target object.

/man/

Because the preceding regular expression pattern ends with a "" Locator, you can match a string that ends with "human", "Woman" or "man" in the target object.

In order to make it easier for users to set the matching pattern, the regular expression allows the user to specify a range in the matching pattern rather than a specific character. For example:

/[a-z]/

The regular expression above will match any uppercase letter from A to Z range.

/[a-z]/

The above regular expression will match any lowercase letter from a to Z range.

/[0-9]/

The regular expression above will match any number in the range 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". One thing you should be reminded of here is that you can use "()" in regular expressions to group strings together. The "()" symbol contains content that must appear in the target object at the same time. Therefore, the preceding regular expression will not match a string such as "ABC", because the last character in "ABC" is a letter rather than a number.

If we want to implement a "or" operation in a regular expression similar to a programming logic, you can use the pipe symbol "|" In any of several different patterns to match. For example:

/to|too|2/

The above regular expression will match "to", "too", or "2" in the target object.

There is also a more commonly used operator in the regular expression, the negative character "[^]". Unlike the locator "^" we described earlier, the negation "[^]" specifies that a string specified in the pattern cannot exist in the target object. For example:

/[^a-c]/

The above string will match any character except A, B, and C in the target object. In general, "^" is considered a negation operator when it appears in "[]", and when "^" is outside of "[]" or "[]", it should be treated as a locator.

Finally, you can use the escape character "" When you want to include metacharacters in the pattern of a regular expression and find the matching object. For example:

/th*/

The regular expression above will match the "th*" in the target object, not the "the", and so on.


http://www.bkjia.com/PHPjc/532606.html www.bkjia.com true http://www.bkjia.com/PHPjc/532606.html techarticle If we ask the enthusiasts of UNIX systems what they like best, the answer is that in addition to a stable system and the ability to remotely launch, nine out of ten people will refer to regular expressions;

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