Common regular expressions (1/2) in asp.net c)

Source: Internet
Author: User

C # Regular expression

It is used for string processing and form verification, and is practical and efficient. Some common expressions are collected here for future use.

If an escape error is prompted, add the @ escape symbol before the regular expression.

If you obtain a regular expression

Method 1: software regexbuddy

Method 2: the. net built-in verification control regularexpressionvalidator is dragged into the webpage, and then the validationexpression in the property behavior is set to have its own common expression copy.

C # is referenced as follows:

The namespace must be introduced:

Using system. text. regularexpressions;

Code
If (! Regex. ismatch (pwd, "^ [A-Za-z0-9] {6, 20} $") {response. write ("<script language = 'webpage effect '> alert ('The length must be greater than 6 characters and less than 20 characters. It can only be English words or numbers, such as snsn2003 ') </script> "); return;} ^ matches the beginning of a row. For example, the regular expression ^ when in can match the start of the string "when in the course of human events", but cannot match "what and when in"
$ Match the row Terminator. For example, the regular expression weasel $ can match the end of the string "he's a weasel"
. Point matches any single character. For example, the regular expression r. t matches these strings: rat, rut, and r t, but does not match root.
* Matches zero or multiple characters that match exactly before it. For example, the regular expression. * means that it can match any number of characters.

 

The matching length must be greater than 6 characters less than 20 characters, only English words, numbers "^ [A-Za-z0-9] {6, 20} $"

Regular Expression Matching Chinese characters: [u4e00-u9fa5]
Note: matching Chinese characters is really a headache. It is easy to use this expression.

Match double byte characters (including Chinese characters): [^ x00-xff]
Comment: It can be used to calculate the length of a string (a dual-byte length meter 2, ascii character meter 1)

Regular Expression Matching blank rows: ns * r
Comment: It can be used to delete blank rows.

Regular expressions matching html tags: <(s *?) [^>] *> .*? </1> | <.*? />
Comments: The versions circulating on the Internet are too bad. The above can only match some of them, and there is still nothing to do with complicated nested tags.

Regular expression matching the first and last blank characters: ^ s * | s * $
Comments: It can be used to delete spaces (including spaces, tabs, page breaks, and so on) at the beginning and end of a line. It is a very useful expression.

Regular Expression Matching the email address: w + ([-+.] w +) * @ w + ([-.] w + )*. w + ([-.] w + )*
Comment: form verification is very useful

Regular Expression Matching url: [a-za-z] +: // [^ s] *
Comments: versions circulating on the Internet have limited functions. The above can basically meet the requirements.

Match account validity (starting with a letter, may be 5-16 bytes, may be an alphanumeric underline): ^ [a-za-z] [A-Za-z0-9 _] {} $
Comment: form verification is very useful

Match Chinese phone number: d {3}-d {8} | d {4}-d {7}
Commentary: matching forms such as 0511-4405222 or 021-87888822

Match Tencent QQ number: [1-9] [0-9] {4 ,}
Comment: Tencent QQ number starts from 10000

Match China Zip code: [1-9] d {5 }(?! D)
Note: China post code is a six-digit number.

Matched ID card: d {15} | d {18}
Note: the ID card of China is 15 or 18 characters

Matched IP address: d +. d +
Comments: useful when extracting IP addresses

Match a specific number:
^ [1-9] d * $ // match a positive integer
^-[1-9] d * $ // match a negative integer
^ -? [1-9] d * $ // match the integer
^ [1-9] d * | 0 $ // match a non-negative integer (positive integer + 0)
^-[1-9] d * | 0 $ // match a non-positive integer (negative integer + 0)
^ [1-9] d *. d * | 0. d * [1-9] d * $ // match the positive floating point number
^-([1-9] d *. d * | 0. d * [1-9] d *) $ // match the negative floating point number
^ -? ([1-9] d *. d * | 0. d * [1-9] d * | 0 ?. 0 + | 0) $ // Match floating point number
^ [1-9] d *. d * | 0. d * [1-9] d * | 0 ?. 0 + | 0 $ // Match non-negative floating point number (positive floating point number + 0)
^ (-([1-9] d *. d * | 0. d * [1-9] d *) | 0 ?. 0 + | 0 $ // match a non-positive floating point number (negative floating point number + 0)
Comments: It is useful when processing large amounts of data. Pay attention to correction when handling specific applications.

Match a specific string:
^ [A-za-z] + $ // match a string consisting of 26 English letters
^ [A-z] + $ // match a string consisting of 26 uppercase letters
^ [A-z] + $ // match a string consisting of 26 lowercase letters
^ [A-Za-z0-9] + $ // match a string consisting of digits and 26 letters
^ W + $ // match a string consisting of digits, 26 English letters, or underscores
Comment: The most basic and commonly used expressions

Homepage 1 2Last page

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.