Recommended: Export Excel files in an instance parsing ASP program One way to use excel in Asp is to link the excel file as a database, and the operation is similar to the access database operation. But this method is not always useful, it should be that excel is not a relational database. For a fixed format,
What is RegExp in ASP
'Name character check
| The following is the quoted content: Public Function CheckName(Str) Checkname=True Dim Rep,pass Set Rep=New RegExp ' Create regular expression. Rep.Global=True 'Set global availability. Rep.IgnoreCase=True' Set whether to distinguish character case-sensitive 'Set mode. Rep.Pattern=[u0009u0020u0022-u0028u002C-u002Eu003A-u003Fu005Bu005Cu0060u007Cu007Eu00FFuE5E5] Set pass=Rep.Execute(Str) 'Perform a regular expression search on the specified string. If pass.count<>0 Then CheckName=False 'response.Write(Checkname) 'response.End() Set Rep=Nothing End Function |
When we make websites, especially various e-commerce websites, we will first ask users to fill out some forms to obtain various information about registered users, because users may enter various information, and some data that does not meet the requirements will cause unnecessary trouble to our back-end ASP processor and even cause some security problems to the website. Therefore, before saving this information to the website database, we must verify the legality of the data input by these users so that the subsequent programs can be executed safely and smoothly. So we usually write an ASP verification program on the backend to analyze whether the data entered by the user is legal.
Some people may ask, can't using JavaScript running on the client to verify user data better and faster? Indeed, this is OK in most cases, why in most cases? Because the JavaScript you write may not be completely normal to run on IE and Netscape at the same time, because Microsoft's Jscript is not completely the same as JavaScript, and some browsers are not necessarily compatible with Microsoft and Netscape, so it is very likely that the Javascript on the client will not accurately verify the various data entered by the user. The ASP program runs on the server side, but it is related to the environment of your server. No matter what browser the client is, there is no difference for your ASP program, so choosing to use the backend ASP program to verify data legality is a good choice.
When using ASP to verify the data legitimacy of the backend, some people have written many functions to implement it to meet the data verification in different environments. For example, if we want to verify whether the URL address entered by the user is legal, we can write a piece of code to analyze the information entered by the user character one by one. If the amount of information to be analyzed is small, it is easier to deal with. If the analysis conditions change, it will be miserable. Not only do we have to write very long and cumbersome code, but the operation efficiency is extremely low. Is there any good solution? Yes, that is the regular expression object provided by VBScriptp5.0. As long as your server has IE5.x installed, it will bring VBScript5.0. In fact, regular expressions were originally patented under Unix, especially in the Perl language. It is precisely because of the powerful functions of regular expressions that Microsoft slowly ports regular expression objects to the window system and uses them.
For regular expression objects, we can easily verify the legality of various data.
First, let's understand what exactly is a regular expression object of VBScript. Let's first look at a program:
| The following is the quoted content: Function CheckExp(patrn, strng) Dim regEx, Match ' Create variables. Set regEx = New RegExp ' Create regular expression. regEx.Pattern = patrn ' Set mode. regEx.IgnoreCase = true ' Set whether character case sensitive. regEx.Global = True ' Set global availability. Matches = regEx.test(strng) ' Perform a search. CheckExp = matches End Function |
In this program, we see that New RegExp can be used to get a regular expression object, and then the object is assigned a regular matching template, that is, telling the regular expression object what kind of template you want to match, and then use the method Test to detect whether the data to be processed matches the template we gave. If it does not match, it means that the data to be processed is not legal data, which realizes the verification of data legality. We can see that using a properly designed matching template, we can easily verify a batch of data information in a format.
However, the regular expression object in VBScript 5.0 has many other methods and properties, such as the method Replace(). Using it, we can quickly implement the fashionable UBB style forum and BBS online. This is not within our scope of discussion. We will discuss it later. Let's take a look at the commonly used methods and properties of regular expression objects in data verification:
Common methods: Execute method
Description: Perform a regular expression search on the specified string.
Syntax: object.Execute(string) The syntax of the Execute method includes the following parts:
object: Required. Always the name of a RegExp object.
string: Required. The text string on which the regular expression is to be executed.
Description: The design pattern of regular expression search is set through the pattern of the RegExp object. 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.
Test method
Description: Performs a regular expression search on the specified string and returns a Boolean value indicating whether a matching pattern was found.
Syntax: object.Test(string)
The syntax of the Test method includes the following parts:
object: Required. Always the name of a RegExp object.
string: Required. The text string to perform a regular expression search.
Note: The actual pattern of regular expression search is set through the Pattern property of the RegExp object. The RegExp.Global property has no effect on the Test method. If a matching pattern is found, the Test method returns True; otherwise, it returns False.
Common properties: Global properties
Description: Sets or returns a Boolean value that indicates whether the pattern matches all or only the first one during the entire search string.
Syntax: object.Global [= True | False ]
The object parameter is always a RegExp object. If the search is applied to the entire string, the Global property has a value of True, otherwise its value is False. The default setting is True.
IgnoreCase attribute
Description: Sets or returns a Boolean value indicating whether the mode search is case sensitive.
Syntax: object.IgnoreCase [= True | False ]
The object parameter is always a RegExp object. If the search is case sensitive, the IgnoreCase property is False; otherwise, True. The default value is True.
Pattern properties
Description: Sets or returns the regular expression pattern being searched. This is the most important attribute, and we mainly set this attribute to achieve data verification.
Syntax: object.Pattern [= searchstring]
The syntax of the Pattern property contains the following parts:
object: Required. Always a RegExp object variable.
searchstring: optional. The regular string expression being searched. It may contain various regular expression characters set in a partial table.
Settings: Special characters and sequences are used when writing patterns for regular expressions. The following table describes the characters and sequences that can be used and gives examples.
Character Description: Mark the next character as a special character or literal value. For example n matches the character n. n matches line breaks. sequence/match, (match.
^: Match the start position of the input.
$: Match the end of the input.
*: Match the previous character zero or several times. For example, zo* can match z and zoo.
: Match the previous character once or more times. For example, zo can match zoo, but not z.
?: Match the previous character zero or once. For example, a?ve? can match ve in never.
.: Match any character other than line breaks.
(pattern) Match the pattern and remember the match. The matching substring can be obtained from the resulting Matches collection using Item [0]...[n]. If you want to match the bracket characters (and), you can use (or).
x|y: Match x or y. For example, z|food can match z or food. (z|f)ood matches zoo or food.
{n}: n is a non-negative integer. Match exactly n times. For example, o{2} cannot match o in Bob, but can match the first two o in Foooood.
{n,} : n is a non-negative integer. Match at least n times. For example, o{2,} does not match o in Bob, but all o in Foooood. o{1,} is equivalent to o . o{0,} is equivalent to o*.
{n,m} : m and n are non-negative integers. Match at least n times, up to m times. For example, o{1,3} matches the first three os in fooooood. o{0,1} is equivalent to o?.
[xyz]: A character set. Matches one of the characters in brackets. For example, [abc] matches a in plain.
[^xyz]: A negative character set. Match any characters not in this bracket. For example, [^abc] can match p in plain.
[az]: represents a character within a certain range. Matches any character within the specified interval. For example, [az] matches any lowercase alphabetical character between a and z.
[^mz]: Negative character interval. Matches characters that are not within the specified interval. For example, [mz] matches any character that is not between m and z.
b: Match the boundary of the word, that is, the position between the word and the space. For example, erb matches er in never, but does not match er in verb.
B: Matching with non-word boundaries. ea*rB matches the ear in never early.
d: Matches a numeric character. Equivalent to [0-9].
D: Matches non-numeric characters. Equivalent to [^0-9].
f: Matches page breaks.
n: Matches line break character.
r: Matches the carriage return character.
s: Matches any white character, including spaces, tabs, page breaks, etc. Equivalent to [fnrtv].
S: Matches any non-blank character. Equivalent to [^ fnrtv].
t: Matches the tab character.
v: Matches vertical tab characters.
w: Matches any word character, including underscore. Equivalent to [A-Za-z0-9_].
W: Matches any non-word character. Equivalent to [^A-Za-z0-9_].
num: matches num, where num is a positive integer. Quote back to remember matches. For example, (.)1 matches two consecutive identical characters.
n: Match n, where n is an octal code transfer value. The octal code transfer value must be 1, 2 or 3 numbers long.
For example, both 11 and 11 match a tab. 011 is equivalent to 01 and 1. The octal code transfer value shall not exceed 256. Otherwise, only the first two characters are considered part of the expression. Allows the use of ASCII code in regular expressions.
xn: Match n, where n is a hexadecimal code transfer value. The hexadecimal code transfer value must be exactly two numbers long. For example, x41 matches A. x041 is equivalent to x04 and 1. Allows the use of ASCII code in regular expressions.
Okay, these are the commonly used methods and attributes. The above syntax is already very detailed, so there is no need to talk about it. Next, let’s take a look at how to use these methods and attributes to verify the legitimacy of the data in specific examples. Let’s give an example. For example, we want to verify the email input by the user. So, what kind of data is considered a legal email? I can enter this: [email protected], of course I will also enter this: [email protected], but such input is illegal: xxx@@com.cn or @xxx.com.cn, etc., so we get a legal email address that should at least meet the following conditions:
1. Must contain one and only one symbol @
2. Must contain at least one at most three symbols.
3. The first character must not be @ or.
4. @.or.@ is not allowed
5. The ending must not be a character @ or.
So based on the above principles and the syntax in the table above, we can easily get the required template as follows: (w) [@]{1}(w) [.]{1,3}(w)
Next, let’s analyze this template carefully. First, w means that the start character of the email can only be a word character containing an underscore, so that the third condition is met; [@]{1} means that the character should be matched in the email and can only be matched once, and the condition one is met; the same [.]{1,3} means that at least 1 matches at most 3 characters in the email. , satisfying the second condition; the last (w) of the template indicates that the ending character can only be a word character containing an underscore, satisfying the condition five; (w) in the middle of the template meets the condition four.
Then, we just call the function CheckExp((w) [@]{1}(w) [.]{1}(w) , the string to be checked). If True is returned, it means that the data is legal, otherwise it will be incorrect. How about it, it is simple. We can also write a template for verifying the ID number: ([0-9]){15}; a template for verifying the URL: ^http://{1}((w) [.]){1,3}, etc.; we can see that these templates provide us with very good reusable modules. Using various templates provided by ourselves or others, we can easily and quickly verify the legality of the data. I believe you will definitely write a very general template.
In this way, we can verify the legality of different data by customizing different templates. Therefore, the most important attribute in the regular expression object is the Pattern attribute. Only by truly mastering this attribute can you freely use the regular expression object to serve our data verification.
Use the same rules
| The following is the quoted content: string str=<img src=/upimg/allimg/081024/0851350.jpg><img src=/upimg/allimg/081024/0851351.jpg><img src=/upimg/allimg/081024/0851352.jpg> Regex reg=new Regex(<imgs src=(['|]?)(S .w )(1)); MatchCollection mc = reg.Matches(input); foreach (Match m in mc) { TB_Result.AppendText(String.Format({0} is matchrn, m.Groups[0])); for (int i = 0; i < m.Groups.Count; i ) { //The image address should be m.Groups[2].Value Response.Write(String.Format(Group[{0}]={1}rn, i, m.Groups[i].Value)); } } |
//How many matches are there
Response.Write(mc.Count.ToString());
Share: How to use the asp program to read the website's Alexa world rankings Whenever Alexa ranking is updated, I need to sort out the rankings of all related similar websites and see the ranking updates of these rival websites. I have done more, and I feel annoyed. Although I have only more than 30 websites, I feel a little tired after watching them one by one. Therefore, I want