Regular expression
If we ask those unix systems, they like what they like, the answer is in addition to the stable system and can be launched remotely, and the ten eight-nine people will mention regular expressions.
If we ask them the most headache, it may be a regular expression in addition to complex process control and installation processes. So what is the regular expression? Such as
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.
beneficial.
Getting started
Simply put, the regular expression is a powerful tool that can be used for pattern matching and replacement. We can find regular tables in almost all UNIX-based tools
Dare-type figure, for example, a VI editor, Perl or PHP scripting language, and awk or sed shell programs. In addition, the scripting language like JavaScript is also available.
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 expressions allow users to build matching mode by using a series of special characters, then input matching mode with data files, program input, and web pages.
The target object is compared, and whether it contains the matching mode in the comparison object, perform the corresponding program.
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 you verify it through regular expression
The format of the household email address is correct, the form information filled in the user will be processed normally; contrary, if the user entered by the user does not match the mode of the email address, it will pop up
Tip information requires the user to 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 only put the pattern content you want to find the matching object "/"
Right between the characters. 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 those in regular expressions.
A special character with a special meaning can be used to specify its preamble character (ie characters located in front of the metammat) in the target object.
More commonly used metamodes include: " ", "*", and "?". Among them, " " figures specify that their preamble characters must have a continuous display in the target object.
Second, "*" figures specify that its leading character must occur zero or continuous multiple times in the target object, "?" Figures specify that its leading object must continue to appear continuously in the target object
Times or once.
Let's take a look at the specific application of the regular expression element character.
/ fo /
Since the above regular expression contains " " element characters, it indicates that "fool", "fo", or "football" can be continuous after the letter F, or "football", or the like can be continuous in the letter F.
The string of the current one or more letter O matches.
/ eg * /
Since the above regular expression contains "*" character, it indicates that "Easy", "EGO", or "EGG", or "EGG", or the like can be continuous in the letter E.
The string of one or more letter G matches.
/ Wil? /
Because of the above regular expression contains "?" Metamorphic, "Will", or "Wilson", or "Wilson" can be used in the letter I, or the alphabetic or "Wilson", or the like.
A string of a letter L matches.
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)
Below, we look at how to use the above metades in the regular expression.
/ 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 his hand, we can find all the total amount of thousands of yuan through the above regular expressions.
In addition to the metamorphors described above, there is another unique dedicated character, ie, locator in the regular expression. The locator is used to specify the matching mode in the purpose.
The appearance position in the target object.
More commonly used locators include: "^", "$", ",", "b". Where "^" locator specifies that the matching mode must appear on the beginning of the target string
The "$" locator specifies that the matching mode must appear on the end of the target object, and the locator specifies that the matching mode must appear on the beginning or end of the target string or one of the two boundaries.
And "B" positioning rules specify that matching objects must be within two boundaries of the beginning and end of the target string, ie the matching objects cannot be the beginning of the target string, nor
The end of the target string. Similarly, we can also regard "^" and "$" and "b" as two sets of locators that are mutually counters. for example:
/ ^ Hell /
Since the "^" locator is included in the above regular expression, it can match the string of "hell", "hello" or "hellhing" in the target object.
.
/ AR $ /
Since the "$" locator is included in the above regular expression, it can match the string ends with "car", "bar" or "ar" in the target object.
/ BOM /
Since the above regular expression mode is in the "" positioner, it can match the string of "BOMB" or "BOM" in the target object.
/ MAN /
Since the above regular expression mode is matched with the target object in the target object, it can match the string of "Human", "Woman" or "Man" in the target object.
In order to facilitate user more flexible setting matching mode, the regular expression allows the user to specify a range in the match mode without being limited to the specific character. E.g:
/ [A-z] /
The above regular expression will match any uppercase from the A to Z.
/ [a-z] /
The above regular expression will match any lowercase alphabet from the A to Z.
/ [0-9] /
The above regular expression will match any of the numbers 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". Here you need to remind users to pay attention to it is to be in the regular table.
The Darn Use "()" combines the string together. "()" The content containing the symbol must appear in the target object at the same time. Therefore, the above regular expression will not be equal to
"ABC" and the like are matched because the last character in "ABC" is a letter instead of numbers.
If we want to implement "or" or "in the regular expression, you can use a pipeline in multiple different modes.
"|". E.g:
/ to | TOO | 2 / The above regular expression will match "to", "TOO", or "2" in the target object.
There is also a more common operator in the regular expression, ie, negative "[^]". Unlike the positioning character "^" introduced by our foregoing, negative "[^]" specified
There is no string specified in the mode in the target object. E.g:
/ [^ A-c] /
The above strings will match any characters other than A, B, and C in the target object. In general, when "^" appears in "[]", it is considered a negative operator.
And when "^" is "[]", or if "[]", it should be regarded as a locator.
Finally, when the user needs to add a metammat in the regular expression of the regular expression, and find the escape character when it looks for match objects. E.g:
/ TH * /
The above regular expression will match "TH *" instead of "THE" or the like in the target object.
Use example
After the regular expression has a more comprehensive understanding, let's take a look at how to use regular expressions in Perl, PHP, and JavaScript.
Typically, the usage format of the regular expression in Perl is as follows:
Operator / regular-expression / string-to-replace / modifiers
One of the operators can be M or S, represent matching operations and replacement operations, respectively.
Among them, the regular expression is a mode that will match or replace the operation, can be composed of any character, element character, or locator. Replace the string one is used
When the S operator, a string that matches the object to the object is matched. The final parameter item is used to control different match or replacement. E.g:
S / geed / good /
The first GEED string will be found in the target object and replace it with a good. If we want to perform multiple times in the global scale of the target object - replacement
If you do, you can use the parameter "g", namely S / Love / Lust / G.
In addition, if we don't need to limit the case where you don't need to limit the matching form, you can use the parameter "I". E.g,
m / jewel / i
The above regular expression will match Jewel, Jewel, or Jewel in the target object.
In Perl, use specialized operators "= ~" to specify matching objects of regular expressions. E.g:
$ FLAG = ~ S / ABC / ABC /
The above regular expression will replace the string ABC in the $ FLAG to ABC.
Below, we add regular expressions in the Perl program to verify the validity of the user's mail address format. code show as below:
#! / usr / bin / perl
# Get input
PRINT "What's your email address?";
$ EMAIL = <>
CHOMP ($ email);
# Match and Display Result
IF ($ email = ~ /^([a-za-z0-9_-] )@([A-ZA-Z0-9_-]) (.[A-ZA-Z0-9_-]) )
{
Print ("Your Email Address Is Correct!");
}
Else
{
Print ("please try!");
}
If the user prefer PHP, you can use an EREG () function to match the mode matching operation. The use format of the EREG () function is as follows:
EREG (Pattern, String)
Among them, Pattern represents the mode of the regular expression, and String is the target object that performs the lookup replacement operation. It is also a verification email address, and the program written in PHP
The code is as follows:
phpif (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!";
?>
Finally, let's take a look at JavaScript. JavaScript 1.2 has a powerful regexp () object that can be used to perform a matching operation of regular expressions.
The test () method can verify that there is a match mode in the target object and return TRUE or FALSE accordingly.
We can write the following script using JavaScript to verify the validity of the mail address entered by the user.
Function VerifyAddress (OBJ)
{
Var email = Obj.email.Value;
Var pattern = /^([A-ZA-Z0-9_-] )@([A-ZA-Z0-9_-]) (.[A-ZA-Z0-9_-])
Flag = Pattern.Test (email);
IF (Flag)
{
Alert ("Your Email Address Is Correct!);
Return True;
}
Else
{
Alert ("please try!");
Return False;
}
}
// stop hiding ->
script>
hEAD>