Application of the positive expression of ASP-1

Source: Internet
Author: User
Tags object contains end expression regular expression variable valid email address access
Regular
   First. Overview of regular Expressions
If you have not used regular expressions before, you may not be familiar with this term or concept. However, they are not as novel as you might think.
Recall how you found the file on your hard disk. Are you sure you will use it? and * characters to help find the file you are looking for. The character matches a single character in the file name, while the * matches one or more characters. One like ' data?. DAT ' mode can be found in the following files: Data1.dat, Data2.dat and so on. If you use the * character instead? Character, the number of files found will be enlarged. ' Data*.dat ' can match all of the following file names: Data.dat, Data1.dat, Data12.dat, and so on, although this method of searching for files is certainly useful but very limited. and * The limited ability of wildcards allows you to have a concept of what regular expressions can do, but regular expressions are more powerful and flexible.
When we write ASP programs, we often judge the validity of a string, such as whether a string is a number, whether it is a valid email address, and so on. If you do not use regular expressions, then the judgment of the program will be very long and error prone, if the use of regular expressions, these judgments is a very easy job. Later we will describe how to determine the validity of digital and email addresses.
In a typical search and replace operation, you must provide the exact text you want to find. This technique may be sufficient for simple search and replace tasks in static text, but because of its lack of flexibility, it is difficult or even impossible to search for dynamic text.
What can be done with regular expressions?
Tests a pattern of a string. For example, you can test an input string to see if there is a phone number pattern or a credit card number pattern in the string. This is known as data validation.
Replaces text. You can use a regular expression in your document to identify specific text, and then you can delete it all, or replace it with another text.
Extracts a substring from a string based on pattern matching. Can be used to find specific text in text or input fields.
For example, if you need to search the entire Web site to remove some outdated material and replace some HTML formatting tags, you can use regular expressions to test each file to see if there are any material or HTML formatting tags that you want to find in the file. With this method, you can narrow the affected file range to those files that contain the material you want to delete or change. You can then use regular expressions to delete obsolete materials, and finally, you can use regular expressions again
To find and replace the tags that need to be replaced.
So, what is the syntax of regular expression syntax?
A regular expression is a literal pattern consisting of ordinary characters (such as characters A through Z) and special characters (called metacharacters). The pattern is described in the lookup text
One or more strings to match when the character body is to be matched. A regular expression is used as a template to match a character pattern with the string being searched for.
Here are some examples of regular expressions that you might encounter:
/^\[\t]*$/"^\[\t]*$" matches a blank line.
/\d{2}-\d{5}/"\d{2}-\d{5}" verifies that an ID number consists of a 2-digit number, a hyphen, and a 5-digit number.
/< (. *) >.*<\/\1>/"< (. *) >.*<\/\1>" matches an HTML tag.

   The application of regular expression in VBScript
VBScript uses the RegExp object, the Matches collection, and the match object to provide regular expression support functionality. Let's look at an example first.
<%
Function regexptest (PATRN, STRNG)
Dim regEx, Match, matches ' Set variable.
Set regEx = New RegExp ' establishes a regular expression.
Regex.pattern = Patrn ' Set mode.
Regex.ignorecase = True ' Sets whether character case is case-sensitive.
Regex.global = True ' Sets global availability.
Set matches = Regex.execute (strng) ' performs a search.
For the match in matches ' traversal matching collection.
Retstr = retstr & "Match found at position"
Retstr = retstr & Match.firstindex & ". Match Value is ' "
Retstr = retstr & Match.value & "'." & "<BR>"
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 that have no is or JS, ignoring the case. The results of the 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 the three objects and collections below.
   1, RegExp objectis one of the most important objects, and it has several attributes, of which:
The 0Global property, sets or returns a Boolean value that indicates whether the pattern matches all or only the first of the entire search string. If the search applies to the entire string, the value of the Global property is True, otherwise its value is False. The default setting is False.
The 0IgnoreCase property, sets or returns a Boolean value that indicates whether the pattern search is case-sensitive. If the search is case-sensitive, the IgnoreCase property is False; otherwise, true. The default value is False.
The 0Pattern property, sets or returns the regular expression pattern that is searched. Required option. Always a REGEXP object variable.
   2, Match object
The result of a matching search is stored in the match object, providing access to the read-only property that matches the regular expression. The match object can only be created by using the RegExp object's Execute method, which actually returns a collection of Match objects. All of the Match object properties are read-only. When you execute a regular expression, you may produce 0 or more Match objects. Each match object provides access to the string found by the regular expression search, the length of the string, and the location of the matching index.
0FirstIndex property that returns the location of the match in the search string. The FirstIndex property uses an offset from zero, which is relative to the
The starting position of the search string. In other words, the first character in the string is identified as a character 0
The 0Length property, which returns the length of the match found in the string search.
A 0Value property that returns the matching value or text found in a search string.
   3, Matches collection
The collection of regular Expression Match objects. The Matches collection contains several separate Match objects that can only be created using the Execute method of the RegExp object. As with the independent Match object properties, one of the properties of the matches ' collection is read-only. When you execute a regular expression, you may produce 0 or more Match objects. Each match object provides an access entry to the string that matches the regular expression, the length of the string, and an index that identifies the matching location.
Learning about these three objects and collections, how do you apply them to the judgment and substitution of strings? The three methods of the RegExp object solve the problem, they are the Replace method, the test method, and the Execute method.
   1. Replace Method
Replaces the text found in the regular expression lookup. Let's take a look at an example: The following example illustrates the use of the Replace method.
<%
Function replacetest (PATRN, REPLSTR)
Dim regEx, str1 ' Set variable.
STR1 = "The quick brown fox jumped over the lazy dog."
Set regEx = New RegExp ' establishes a regular expression.
Regex.pattern = Patrn ' Set mode.
Regex.ignorecase = True ' Sets whether case sensitive.
Replacetest = Regex.Replace (str1, Replstr) ' for replacement.
End Function
Response.Write Replacetest ("Fox", "Cat") & "<BR>" Replaces ' Fox ' with ' cat '.
Response.Write Replacetest ("(\s+) (\s+) (\s+)", "$3$2$1") ' exchange of words.
%>
   2. Test Method
Performs a regular expression search on the specified string and returns a Boolean indicating whether the matching pattern is found. The actual mode of the regular expression search is set by regexp the Pattern property of the object. The Regexp.global property has no effect on the test method.
The test method returns true if a matching pattern is found, otherwise it returns false. The following code illustrates the use of the test method.
<%
Function regexptest (PATRN, STRNG)
Dim regEx, RetVal ' Set variable.
Set regEx = New RegExp ' establishes a regular expression.
Regex.pattern = Patrn ' Set mode.
Regex.ignorecase = False ' Sets whether case sensitive.
RetVal = regex.test (strng) ' performs a search test.
If RetVal Then
Regexptest = "Find one or more matches. "
Else
Regexptest = "no match found. "
End If
End Function
Response.Write Regexptest ("is.", "IS1 is2 IS3 is4")
%>
   3. Execute Method
Performs a regular-expression search on the specified string. The design pattern of a regular expression search is set by RegExp object patterns.
The Execute method returns a matches collection that contains each matching match object found in string. If no match is found, Execute returns an empty matches collection.



Related Article

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.