If we ask those unix systems, they like what they like. In addition to stable systems and can be started remotely, ten eight-nine people will mention regular expressions; if we ask what they are the most, what is the most headache? In addition to complex process control and installation procedures, it will also be regular expressions. So what is the regular expression? How can I really master the regular expression and properly use it? This article will introduce this, hoping to help readers who are eager to understand and master regular expressions.
Getting started
Simply put, the regular expression is a powerful tool that can be used for pattern matching and replacement. We can find a regular expression in almost all UNIX-based tools, such as a VI editor, Perl, or PHP scripting language, and awk or sed shell programs. In addition, the scripting language like JavaScript has also provided support for regular expressions. It can be seen that the regular expression has exceeded the limitations of some languages or a system, and has become a widely accepted concept and function.
Regular expression allows users to build a matching mode by using a series of special characters, then compare the matching mode with data files, program input, and web pages, whether or not to include matching mode in the comparison object, perform corresponding program of.
For example, a general expression of a regular expression is whether it is used to verify that the format of the mail address entered online input is correct. If the format of the user mail address is verified by the regular expression, the form information filled out will be processed normally; contrary, if the user entered by the user input does not match the mode, the prompt information will be popped up, requiring the user to re-re- Enter the correct email address. This shows that the regular expression has a pivotable role in the logical judgment of the web application.
Basic syntax
After a preliminary understanding of the function and function of the regular expression, we will see the syntax format of the regular expression.
The form of regular expressions is generally as follows:
/ Love /
The part between the "/" delimiter is the mode that will be matched in the target object. Users can put them between the mode content you want to find the matching object in the "/" delimiter. In order to be able to make user more flexible custom mode content, regular expressions provide special "metadamic characters". The so-called metammatism refers to the exhibit mode of its preamble characters (i.e., characters in front of the metamorphism) in the regular expression.
More commonly used metamodes include: " ", "*", and "?". Among them, " " figures specify that its predetermined characters must continue once or more in the target object, "*" element character specifies that its predetermined character must occur zero or continuous in the target object, and "?" Yuan Characters are specified that their leading objects must be zero or once in the target object.
Let's take a look at the specific application of the regular expression element character.
/ fo /
Since the above regular expression includes a " " element character, indicating that "fool", "fo", or "football" in the target object can match the string of one or more letter O after the letter f after the letter F. .
/ eg * /
Since the above regular expression contains "*" character, it indicates that "EASY", "EGO", or "EGG" in the target object can continuously appear from zero or more letter Gs after the letter E. match.
/ Wil? /
Because of the above regular expression contains "?" Metad character, it indicates that "WILL", or "Wilson" in the target object, or the like, the string of zero or one letter L continuously after the letter I.
In addition to the metammat, the user can accurately specify the frequency that appears in the match object. E.g,
/ jim {2,6} /
The above regular expression specifies that the character m can continuously appear in two times in the matching object, and therefore, the regular expression may match the character string such as JIMMY or JIMMMMMY. After you have a preliminary understanding of how to use the regular expression, let's take a look at the other important metades.
S: Used to match a single space character, including Tab keys, and wrap;
S: Used to match all characters except for single space characters;
D: Used to match the number 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 outside of the resort.
(Note: We can regard S and S and W and W as mutual retrograms)