Application of regular expressions in ASP

zhaozj2021-02-16  193

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 tags 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 looking for a text body. 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 = retrstr & match.value & "'." & "
" Next Regexptest = Retstr End Function Response .write regexptest ("[ij] s", "IS1 JS2 IS3 IS4")%> In this example, we look for two words in the string, IS or JS, ignore the case. The results of operation 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. 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 function response.write replacestest ("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 THEN regexptest = "Find one or more match." Else regexptest = "did not find a match." End if End Function Response.Write Regexptest ("IS.", "IS1 IS2 IS3 IS4")%> 3, Execute method Perform regular expressions search for the specified string. Regular expression search design mode is set by the Pattern of the Regexp object. 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 an empty Matches collection. Third, the use of regular expressions in JavaScript After the JavaScript version 1.2, JavaScript also supports regular expressions. 1. Replace Replace Find replaces the corresponding content through the regular expression in a string. Replace does not change the original string, just regenerating a new string. If you need to perform global lookups or ignore your case, you will add G and I for the last addition of the regular expression. Example: