Regular expressions

xiaoxiao2021-03-06  71

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? /

Since "?" Metad characters are included in the above regular expression, it indicates that "WIN", or "Wilson" in the target object, or a string of zero or one letter L continuous 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 outside of single spaces;

/ 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 counterputting)

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 appearance of the matching mode in the target object.

More commonly used locators include: "^", "$", "/ b", and "/ b". Where "^" positioning specifies that the match mode must appear at the beginning of the target string, the "$" locator specifies that the match mode must appear on the end of the target object, / b Locator specified that the match mode must appear on the start of the target string Or one of the two boundaries end, and "/ b" positioning rules that match objects must be within two boundies of the start and end of the target string, ie the matching objects cannot be the beginning of the target string, and cannot be used as The end of the target string. Similarly, we can also regard "^" and "$" and "/ b" and "/ b" as two sets of locators that are inversely. for example:

/ ^ Hell /

Since the above regular expression contains "^" locator, 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.

// bbom /

Since the above regular expression mode begins with the "/ b" positioner, it can match the string beginning with "bomb", or "bom" in the target object.

/ MAN / B /

Because the above regular expression mode is tailing in "/ b", it can match the string of "hum", "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, it is necessary to remind the user to pay attention to the use of "()" to combine the string in the regular expression. "()" The content containing the symbol must appear in the target object at the same time. Therefore, the above 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 "or" or "operations in the regular expression, you can use a match in multiple different modes to use the pipeline" | ". 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 locator "^" described in our forebel, negative "[^]" specifies the 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 "[]", it should be regarded. Locator.

Finally, when the user needs to add a metamorphic in the regular expression of the regular expression and find the matching object, you can use the escape character "/". E.g:

/ TH / * /

The above regular expression will match "TH *" instead of "THE" or the like in the target object.

Application of regular expressions in ASP (1)

I. Overview of regular expressions II. Application of regular expression in VBScript three, regular expressions in Vavascript, four, example 5, summary

1. General Expression Overview If you have not used a regular expression, you may be less familiar with this terminology and concept. However, they are not so nice you imagine. Recall how to find files on the hard disk. You will definitely use the * characters to help find the files you are looking for. • Characters match a single character in the file name, and * matches one or more characters. A pattern such as 'Data ?dat' can find the following files: Data1.dat, Data2.dat, etc. If you use * characters instead? The number of files found will be expanded. 'data * .dat' can match all the following file names: data.dat, data1.dat, data12.dat, etc., although this search file is definitely useful, it is also very limited. • The limited capacity of wildcards can make you have a concept of regular expressions, but the regular expression is more powerful, and more flexible. When we write an ASP program, the validity of a string often determines, such as whether a string is a number, whether it is a valid email address, and the like. If you do not use a regular expression, the program that judges will be very long, and it is easy to make mistakes. If you use the regular expression, these judgments are a very easy task. Behind we will show how to determine the validity of the numbers and email addresses. In a typical search and alternative, the exact text to be found must be provided. This technique may be sufficient for simple search and replacement tasks in static text, but because it lacks flexibility, it is difficult to search for dynamic text, or even impossible. What is the use of regular expressions? Test a pattern of strings. For example, an input string can be tested to see if the string exists or a credit card number mode. This is called data validity verification. Replace the text. You can use a regular expression in the document to identify a particular text, then you can delete it, or replace it with another text. Extract a sub-string from the string based on the mode match. Can be used to find a specific text in the text or input field. For example, if you need to search the entire Web site to delete some excessive materials and replace some HTML formatted tags, you can use the regular expression to test each file, see if there is a material or HTML you want to find in this file. Formatted tag. With this method, you can narrow the affected file range to those files that contain materials to be deleted or changed. You can then use the regular expression to delete the outdated material, and finally, you can use the regular expression again to find and replace those markers that need to be replaced. So, how is the syntax of the regular expression syntax? A regular expression is a text mode composed of normal characters (such as characters a to z) and special characters (called metammatics). This mode describes one or more strings to be matched when the text body is looking for. Regular expression As a template, a character mode matches the search string. Here are some regular expressions that may encounter: / ^ / [/ t] * $ / "^ / [/ t] * $" matches a blank line. // D {2} - / d {5} / "/ d {2} - / d {5}" Verify that one ID number is composed of a 2-digit, a hyphen, and a 5-digit. /< (.*)>. (*)>. * "matches an HTML tag.

Second, regular expression in VBScript Application VBScript uses the regexp object, Matches collection, and Match objects provide regular expression support. We still look at an example. <% Function Regexptest (PATRN, STRNG) DIM Regex, Match, Matches' establishes variables. Set regex = new regexp 'establishes regular expressions. Regex.pattern = PATRN 'Setting mode. Regex.ignoreCase = true 'Set whether you distinguish between characters. Regex.global = true 'Sets global availability. Set matches = regex.execute (strng) 'Executes your search. FOR Each Match In Matches' traverses matching collection. RetStr = RetStr & "Match found at position" RetStr = RetStr & Match.FirstIndex & ". Match Value is '" RetStr = RetStr & Match.Value & "'." & "
" NextRegExpTest = RetStrEnd Functionresponse.write RegExpTest ("[ij] s.", "IS1 JS2 IS3 IS4")%> In this example, we look for two words that are IS or JS in the string, ignore the case. The results of the run are as follows: Match Found At position 0. Match value is' is1'.match found at position 4. Match value is' js2'match found at position 8. Match value is' is3'.match found at position 12. Match value is 'IS4'. Let's introduce these three objects and collections. 1, the regexp object is the most important object, which has several properties, where: ○ Global property, setting or returns a Boolean value, which indicates that all the match is all matching or only the first one. If the search is applied to the entire string, the value of the Global property is true, otherwise its value is false. The default setting is false. ○ IgnoreCase property, setting, or returns a boolean value, indicating whether the mode search is case sensitive. If the search is case sensitive, the ignorecase property is false; otherwise true. The default is false. ○ Pattern property, setting, or returning the searched regular expression mode. required. Always a regexp object variable. 2. The result of matching the match of Match object is to store access to the read-only attribute that matches the regular expression in the MATCH object. The MATCH object can only be created by the EXECUTE method of the Regexp object, which actually returns a collection of Match objects. All match object properties are read-only. When performing regular expressions, zero or more Match objects may occur. Each Match object provides access, the length of the string search, the length of the string, and the matching index position, and the like. ○ The firstIndex property returns a location that matches in the search string.

The firstIndex property uses the offset from zero, which is relative to the starting position of the search string. In other words, the first character in the string is identified as character 0 ○ Length attribute, returns the length of the match found in the string search. ○ Value property returns the matching value or text found in a search string. 3, Matches Collection Regular Expression Match object collection. The Matches collection contains a number of separate Match objects that can only be created with the EXECUTE method of the regexp object. The same property of the Matches `collection is read-only. An attribute of the Matches` Collection is read-only. When performing regular expressions, zero or more Match objects may occur. Each MATCH object provides access portions, strings, and indexes of identifying matching positions with strings that match regular expressions. Learn these three objects and collections, how to apply the judgment and replacement of a string? The three methods of the regexp object have solved this problem, they are the Replace method, Test method, and Execute method. 1. Replacing method replaces the text found in the regular expression lookup. We still look at an example: The following example illustrates the usage of the Replace method. <% Function Replacestest (PATRN, REPLSTR) DIM Regex, str1 'establishes variables. STR1 = "The Quick Brown Fox Jumped Over The Lazy Dog." SET Regex = New Regexp 'establishes a regular expression. Regex.pattern = PATRN 'Setting mode. Regex.ignoreCase = true 'Set whether you are case sensitive. Replacest = regex.Replace (str1, replstr) 'is replaced. End functionResponse.write replatest ("fox", "cat") & "
" replaces 'Fox' to 'CAT'. Response.Write Replacestest ("(/ S ) (/ S )", "$ 3 $ 2 $ 1") 'exchange word pair.%> 2, TEST method performs a regular expression search for the specified string, and Returns a Boolean value indicates whether a matching mode is found. The actual mode of the regular expression search is set by the Pattern property of the Regexp object. Regexp.global properties have no effect on the Test method. If a matching mode is found, the Test method returns true; otherwise returns false. The following code illustrates the usage of the Test method. <% Function Regexptest (PATRN, STRNG) DIM Regex, RetVal 'establishes variables. Set regex = new regexp 'establishes regular expressions. Regex.pattern = PATRN 'Setting mode. Regex.ignoreCase = false 'Set whether you are case sensitive. Retval = regex.test (strng) 'Performs a search test. If RETVAL THENREGEXPTEST = "Find one or more match." ElseRegexptest = "did not find a match.

"END IFEND FUNCTIONRESPONSE.WRITE REGEXPTEST (" IS. "," IS1 IS2 IS3 IS4)%> 3, Execute method performs regular expression search for the specified string. Regular expression search design mode is Pattern through the regexp object To set it. The execute method returns a Matches collection, which contains each matching match object found in the String. If matching is not found, Execute will return empty matches collection. Third, regular expressions in JavaScript use in JavaScript After the 1.2 version, JavaScript also supports regular expressions. 1, Replace Replace Find the corresponding content in a string. Replan is not changed. Replan does not change the original string, just regenerates a new string. If It is necessary to perform global lookup or ignore the case, then the last addition of g and i in regular expressions, the g and i. Example: