The use of regular expressions in the detailed

Source: Internet
Author: User
Tags regular expression

If we ask fans of UNIX systems what they like best, in addition to stable systems and the possibility of remotely booting, ten people will mention regular expressions, and if we ask them what the headaches are, they might be regular expressions in addition to the complex process control and installation process. So what exactly is a regular expression? How can we really grasp the regular expression and use it flexibly? This article will be introduced in this paper, hoping to be useful for those who are eager to understand and grasp the regular expression of the reader.


Introduction to Getting Started

In short, regular expressions are a powerful tool that can be used for pattern matching and substitution. We can find regular expressions in almost all the Unix-based tools, such as the VI editor, the Perl or PHP scripting language, and awk or sed shell programs. In addition, the scripting language of the client, like javascript/"target=" _blank, also provides support for regular expressions. Thus, regular expressions have gone beyond the limits of a language or a system and become a widely accepted concept and function.

Regular expressions allow the user to build a matching pattern by using a series of extraordinary characters, then compare the matching pattern with the target objects such as data file, program input, and form input of the Web page, and execute the corresponding program according to whether the matching pattern is included in the comparison object.

For example, one of the most common applications of regular expressions is to verify that the e-mail addresses that users enter online are correctly formatted. If the user's email address is properly formatted through regular expressions, the form information that the user fills out will be processed normally, otherwise, if the user enters the email address that does not match the regular expression pattern, a prompt will pop up asking the user to re-enter the correct email address. This shows that regular expressions play an important role in the logical determination of Web applications.


Basic syntax

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

Regular expressions are generally as follows:

/love/

The part of the "/" delimiter is the pattern that will be matched in the target object. The user simply puts the pattern content that wants to find the matching object in between the "/" delimiters. Regular expressions provide specialized "meta characters" to enable users to customize schema content more flexibly. The term "metacharacters" refers to those special characters that have extraordinary meaning in regular expressions and can be used to specify the mode in which the leading character (that is, the character at the front of the metacharacters) appears in the target object.

The more commonly used meta characters include: "", "*", and "?". Where the "" metacharacters specify that their leading characters must be in the target object? One or more times, the "*" metacharacters specify that its leading characters must appear 0 or more times in the target object, and the "?" Metacharacters specify that its leading object must appear 0 or one consecutive times in the target object.

Next, let's look at the specific application of regular expression meta characters.

/FO/

Because the preceding regular expression contains the "" metacharacters, it means that a string of one or more letters O can be matched with the "fool", "fo", or "football" in the target object after the letter F.

/eg*/

Because the above regular expression contains a "*" metacharacters, the representation can match the string of 0 or more letters g that are "easy", "ego", or "egg" in the target object, which follows the letter E.

/wil?/

Because the above regular expression contains "? A meta character that matches a string of 0 or one letter L that can occur consecutively after the letter I in the target object, such as "Win" or "Wilson".

In addition to metacharacters, users can specify exactly how often a pattern will appear in a matching object. For example

/jim{2,6}/

The regular expression above stipulates that the character M can appear consecutively 2-6 times in a matching object, so the regular expression above can match a string such as Jimmy or Jimmmmmy.

After a preliminary understanding of how to use regular expressions, let's look at how other important metacharacters are used.

S: Used to match a single spaces, including tab keys and line breaks;

S: Used to match all characters except a single spaces;

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 for line breaks.

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

Below, let's take a look at how to use the above metacharacters in regular expressions.

/S/

After a more comprehensive understanding of regular expressions, let's take a look at how regular expressions are used in perl,php, and javascript/"target=" _blank >javascript.

In general, the use format of the Perl regular expressions is as follows:

Operator/regular-expression/string-to-replace/modifiers

An operator can be an M or S, representing a matching operation and a substitution operation, respectively.

In which, a regular expression is a pattern that will be matched or replaced, and can consist of any character, meta character, or locator symbol. The replacement string is a string that replaces the found pattern matching object when the S operator is used. The final parameter items are used to control different ways of matching or replacing. For example:

s/geed/good/

The first occurrence of the geed string will be found in the target object and replaced with the good. If we want to perform multiple lookups within the global scope of the target object

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.