Extract the src path of the picture in HTML

Source: Internet
Author: User
Tags format object character set end expression integer string variable

What is regexp in ASP

' Name character test

The following are the referenced contents:
Public Function CheckName (STR)
Checkname=true
Dim Rep,pass
Set Rep=new RegExp ' establishes regular expressions.
Rep.global=true ' Sets global availability.
Rep.ignorecase=true ' Sets whether to differentiate character case
' Set mode.
Rep.pattern= "[U0009u0020u0022-u0028u002c-u002eu003a-u003fu005bu005cu0060u007cu007eu00ffue5e5]"
Set Pass=rep.execute (STR) performs 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 a website, especially a variety of e-commerce sites, first of all, we will allow users to fill out a number of forms to obtain registered users of various information, because users can enter a wide range of information, and some do not meet the requirements of the data will give our back-end ASP processing process unnecessary trouble, Even caused some security problems on the site. Therefore, before we save this information to the database of the website, we should validate the data of the information entered by these users so that the subsequent programs can be executed safely and smoothly. So we typically write an ASP's validator at the back end to analyze whether the data entered by the user is legitimate.

Perhaps someone might ask, is it possible to use JavaScript that runs on the client to verify the user's data better and faster? Indeed, this is possible in most cases, why is it most of the case? Because the javascript you write doesn't necessarily run on IE and Netscape at the same time as Microsoft's JScript is not entirely the same as JavaScript, plus some browsers are not necessarily compatible with Microsoft and Netscape, So it is likely that the client's JavaScript will not accurately verify the user input of various data, and the ASP program is running on the server side, but with your server environment, regardless of what the client browser, for your ASP program is no difference, Therefore, it is a good choice to use the backend ASP program to validate the data legality.

When using ASP to verify the validity of the data in the backend, some people to meet the different environment under the data validation, wrote a lot of functions to achieve, for example, we want to verify the user input URL address is legitimate, you can write a piece of code to individual character analysis of the user input information, To analyze the amount of information is small, it is better to do, if the analysis of the changing conditions, that can be miserable, not only to write a very long and cumbersome code, and the efficiency of the operation is extremely low, there is no good solution? Yes, that's the "Regular expression" object provided by VBScritp5.0, as long as your server has ie5.x installed, it will bring VBScript5.0. In fact, the "regular expression" is originally a patent under Unix, especially in the Perl language is the most widely used, it is because of "regular expression" powerful features, so that Microsoft slowly transfer the regular expression object to the Windows system, the use of

Regular Expression object, we can easily validate the validity of various data.

First, let's take a look at what the "regular expression" object is in VBScript, let's start with a procedure:

The following are the referenced contents:

Function checkexp (PATRN, STRNG)

Dim regEx, Match ' establishes the 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.

matches = regex.test (strng) ' performs the search.

Checkexp = Matches

End Function

In this program, we see that we can use "New RegExp" to get a regular expression object, and then make a regular matching template assignment to that object, which tells the regular expression object what kind of template you want to match. Then use method test to detect whether the data to be processed and we give the template is matched, if not match, it indicates that the data to be processed is not legitimate data, thus achieving the validation of data legality, we can see that, using a reasonable design matching template, We can easily verify a batch of similar format data information.

However, the regular expression object in VBScript5.0 has many other methods and properties, such as method replace (), so that we can quickly realize the current online very fashionable UBB style forum and BBS, which is not within the scope of our discussion, to be discussed later, Let's take a look at it. Methods and properties commonly used in data validation for regular expression objects:

Common method: Execute method

Description: Performs a regular-expression search on the specified string.

Syntax: Object. The syntax of the Execute (String) Execute method includes the following sections:

Object: Required. Always the name of a RegExp object.

String: Required. The text string on which to execute the regular expression.

Description: The design pattern of regular expression search is set by RegExp object patterns. The Execute method returns a

The Matches collection, which 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 indicating whether the matching pattern is found.

Syntax: Object. Test (String)

The syntax of the Test method includes the following sections:

Object: Required. Always the name of a RegExp object.

String: Required. The text string to perform a regular expression search for.

Description: The actual pattern of the regular expression search is set by the RegExp object's Patterns property. 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.

Common Properties: Global properties

Description: Sets or returns a Boolean value that indicates whether the pattern matches all or only the first one throughout the search string.

Syntax: Object. Global [= True | False]

The object argument is always REGEXP objects. 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 True.

IgnoreCase Property

Description: Sets or returns a Boolean value that indicates whether the pattern search is case-sensitive.

Syntax: Object. IgnoreCase [= True | False]

The object argument 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 that is searched. This is one of the most important attributes, and we mainly set this property to implement data validation.

Syntax: Object. pattern [= "searchstring"]

The syntax of the pattern attribute consists of the following sections:

Object: Required. Always a REGEXP object variable.

SearchString: Optional. The regular string expression being searched. It may contain various regular expression characters in the Set section 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 an example.

Character Description:: Marks the next character as a special character or literal. For example, "n" matches the character "n". "N" matches the line feed character. The sequence "\" matches "," ("matches" ().

^: matches the start position of the input.

$: Matches the end of the input.

*: Matches the previous character 0 times or several times. For example, "zo*" can Match "Z", "Zoo".

+: Matches the previous character one or more times. For example, "zo+" can Match "zoo", but does not match "Z".

? : matches the previous character 0 times or once. For example, "A?ve?" Can match "never" in the "VE".

.: matches any character other than the newline character.

(pattern) matches patterns and remembers matches. The matching substring can be used with the Item [0] from the matches collection as the result ... [n] obtained. If you want to match bracket characters (and), you can use "(or").

X|y: Matches 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. Matches 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 matches 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, at most m times. For example, "o{1,3}" matches the first three o in "Fooooood". "o{0,1}" is equivalent to "O?".

[XYZ]: a character set. Matches one of the characters in parentheses. For example, "[ABC]" matches "a" in "plain".

[^XYZ]: a negative character set. Matches any character that is not in this bracket. For example, "[^ABC]" can match "P" in "plain".

[A-Z]: represents a range of characters. Matches any character within the specified interval. For example, "[A-z]" matches any of the lowercase alphabetic characters between "a" and "Z".

[^m-z]: a negative character range. Matches characters that are not in the specified interval. For example, "[M-z]" 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: Match the non word boundary. "EA*RB" matches the "ear" in "Never early".

D: matches a numeric character. equivalent to [0-9].

D: Matches characters that are not numeric. equivalent to [^0-9].

F: Matches page breaks.

N: matches the line break character.

R: matches the carriage return character.

S: matches any white character, including spaces, tabs, page breaks, and so on. Equivalent to "[Fnrtv]".

S: Matches any non-white-space character. Equivalent to "[^ Fnrtv]".

T: Matches tabs.

V: Matches the vertical tab.

W: matches any word characters, including underscores. 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. References are returned to the remembered match. For example, "(.) 1 "matches two consecutive identical characters.

N: Match N, where n is a octal value. The octal change value must be 1, 2 or 3 digits long.

For example, "11" and "11" All match a tab. "011" is equivalent to "01" and "1". The value of the octal converter cannot exceed 256. Otherwise, only the first two characters are considered part of an expression. Allows ASCII code to be used in regular expressions.

Xn: matches n, where n is a hexadecimal code-changing value. The hexadecimal transposition value must be exactly two digits long. For example, "x41" matches "A". "x041" is equivalent to "x04" and "1". Allows ASCII code to be used in regular expressions.

Okay, commonly used methods and attributes are these, the above syntax is very detailed, we do not need to wordy, and then we look at the specific examples of how to use these methods and attributes to verify the legality of the data, we still give an example, for example, We want to verify the email that the user entered, so what kind of data is a legitimate email? I can enter this way: uestc95@263.net, of course I will enter this way: xxx@yyy.com.cn, but such input is illegal: xxx@ @com. cn or @xxx.com.cn, etc., so we come up with a legitimate email address that should meet at least several of the following conditions:

1. Must contain one and only one symbol "@"

2. Must contain at least one maximum of three symbols "."

3. The first character must not be "@" or "."

4. "@." is not allowed to appear. Or. @

5. The end must not be the character "@" or "."

So based on the above principles and the syntax in the table above, it's easy to get the template you need: "(W) +[@]{1} (W) +[.] {1,3} (W) + "

Next, we'll look at this template, first "w" means that the beginning character of a message can only be a word character that contains an underscore, so that the third condition is met; [@]{1}] means that it should match in an e-mail message and can only match the character "@", satisfying condition one; {1,3} "means to match at least 1 matches of up to 3 characters in an e-mail message.", satisfies the second condition, the final "(W) +" Character of the template can only be a word character that contains an underscore, satisfies the condition five; the middle of the template "(W) +" satisfies the condition four.

Then we just call that function Checkexp ("(w) +[@]{1} (W) +[." {1} (W) + ", the string to be validated) is good, if the return of true means that the data is legitimate, otherwise it is not correct, how, simple. We can also write a template that verifies the ID number: "([0-9]) {15}"; Template for validating URLs: "^http://{1} ((W) +[.]) {1,3} "and so on; we can see that these templates provide us with a good reusable module, using their own or other provided by the various templates, we can easily and quickly verify the validity of the data, I believe you will write a very common template.

In this way, as long as we customize different templates, we can achieve the validity of different data validation. Therefore, the most important attribute of the regular expression object is: "pattern" attribute, as long as the real grasp of this attribute, you can freely use regular expression object to our data validation services.

The same is true with regular

The following are the referenced contents:

String str= "< IMG src=/upimg/allimg/081024/0851352.jpg> "
Regex reg=new Regex ("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 ());



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.