Common C # Regular Expressions

Source: Internet
Author: User
Common C # regular expressions!

Source: Viewpoint design 8see.net
Http://blog.8see.net/

"^/D + $" // non-negative integer (positive integer + 0)
"^ [0-9] * [1-9] [0-9] * $" // positive integer
"^ (-/D +) | (0 +) $" // non-positive integer (negative integer + 0)
"^-[0-9] * [1-9] [0-9] * $" // negative integer
"^ -? /D + $ "// integer
"^/D + (/./d + )? $ "// Non-negative floating point number (Positive floating point number + 0)
"^ ([0-9] + /. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] */. [0-9] +) | ([0-9] * [1-9] [0-9] *) $ "// Positive floating point number
"^ (-/D + (/./d + )?) | (0 + (/. 0 + )?)) $ "// Non-Positive floating point number (negative floating point number + 0)
"^ (-([0-9] + /. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] */. [0-9] +) | ([0-9] * [1-9] [0-9] *) $ "// negative floating point number
"^ (-? /D +) (/./d + )? $ "// Floating point number
"^ [A-Za-Z] + $" // a string consisting of 26 English letters
"^ [A-Z] + $" // a string consisting of 26 uppercase letters
"^ [A-Z] + $" // a string consisting of 26 lowercase letters
"^ [A-Za-z0-9] + $" // string consisting of digits and 26 letters
"^/W + $" // a string consisting of digits, 26 letters, or underscores
"^ [/W-] + (/. [/W-] +) * @ [/W-] + (/. [/W-] +) + $ "// email address
"^ [A-Za-Z] +: // (/W + (-/W + )*)(/. (/W + (-/W + )*))*(/? /S *)? $ "// URL
/^ (D {2} | D {4})-(0 ([1-9] {1}) | (1 [1 | 2]) -([0-2] ([1-9] {1}) | (3 [0 | 1]) $ // year-month-day
/^ (0 ([1-9] {1}) | (1 [1 | 2]) /([0-2] ([1-9] {1}) | (3 [0 | 1]) /(d {2} | D {4}) $ // month/day/year
"^ ([W-.] +) @ ([0-9] {1, 3 }. [0-9] {1, 3 }. [0-9] {1, 3 }.) | ([w-] + .) +) ([A-Za-Z] {2, 4} | [0-9] {1, 3}) (]?) $ "// Emil
"(D + -)? (D {4 }-? D {7} | D {3 }-? D {8} | ^ d {7, 8}) (-D + )? "// Phone number
"^ (D {1, 2} | 1dd | 2 [0-4] d | 25 [0-5]). (d {1, 2} | 1dd | 2 [0-4] d | 25 [0-5]). (d {1, 2} | 1dd | 2 [0-4] d | 25 [0-5]). (d {1, 2} | 1dd | 2 [0-4] d | 25 [0-5]) $ "// ip address

YYYY-MM-DD basically takes into account the situation of the Year of the leap and February.
^ (1 [6-9] | [2-9]/d)/d {2})-(0? [1, 13578] | 1 [02])-(0? [1-9] | [12]/d | 3 [01]) | (1 [6-9] | [2-9]/D) /d {2})-(0? [13456789] | 1 [012])-(0? [1-9] | [12]/d | 30) | (1 [6-9] | [2-9]/d)/d {2 }) -0? 2-(0? [1-9] | 1/d | 2 [0-8]) | (1 [6-9] | [2-9]/D) (0 [48] | [2468] [048] | [13579] [26]) | (16 | [2468] [048] | [3579] [26]) 00)-0? 2-29-) $

C # Regular Expression
Image SRC [^>] * [^/]. (? : JPG | BMP | GIF )(? :/"| /')
Chinese ^ ([/u4e00-/u9fa5] + | [a-zA-Z0-9] +) $
URL "/<A. +? Href = ['""] (?! HTTP /:////)(?! Mailto/:) (?> Foundanchor> [^ '">] + ?) [^>] *? />"

Regular Expression matching Chinese characters: [/u4e00-/u9fa5]

Match double-byte characters (including Chinese characters): [^/x00-/xFF]

Regular Expression for matching empty rows:/n [/S |] */R

Regular Expressions matching HTML tags:/<(. *)>. * </1> | <(. *)/>/

Regular Expressions matching spaces at the beginning and end: (^/S *) | (/S * $) (TRIM functions like VBScript)

Match the regular expression of the email address:/W + ([-+.] /W +) * @/W + ([-.] /W + )*/. /W + ([-.] /W + )*

The regular expression matching the URL:Http ://([/W-] +/.) + [/W-] + (/[/W -./? % & =] *)?
---------------------------------------------------------------------------
The following is an example:

Use regular expressions to restrict text box input in a webpage form:

You can only enter Chinese characters using regular expressions: onkeyup = "value = value. replace (/[^/u4e00-/u9fa5]/g, '')" onbeforepaste = "clipboardData. setdata ('text', clipboardData. getdata ('text '). replace (/[^/u4e00-/u9fa5]/g ,''))"

1. you can only enter the full-width characters: onkeyup = "value = value. replace (/[^/uff00-/Uffff]/g, '')" onbeforepaste = "clipboardData. setdata ('text', clipboardData. getdata ('text '). replace (/[^/uff00-/Uffff]/g ,''))"

2. use a regular expression to limit that only numbers can be entered: onkeyup = "value = value. replace (/[^/d]/g, '')" onbeforepaste = "clipboardData. setdata ('text', clipboardData. getdata ('text '). replace (/[^/d]/g ,''))"

3. you can only enter numbers and English letters using regular expressions: onkeyup = "value = value. replace (/[/W]/g, '')" onbeforepaste = "clipboardData. setdata ('text', clipboardData. getdata ('text '). replace (/[^/d]/g ,''))"

4. Calculate the length of a string (two-byte length Meter 2, ASCII character meter 1)

String. Prototype. Len = function () {return this. Replace ([^/x00-/xFF]/g, "AA"). length ;}

5. JavaScript does not have trim functions like VBScript. We can use this expression to implement it, as shown below:

String. Prototype. Trim = function ()
{
Return this. Replace (/(^/S *) | (/S * $)/g ,"");
}

Use regular expressions to break down and convert IP addresses:

6. The following is a javascript program that uses regular expressions to match IP addresses and convert IP addresses to corresponding values:

Function ip2v (IP)
{
Re =/(/d +)/. (/d +)/g // The regular expression that matches the IP address
If (Re. Test (IP ))
{
Return Regexp. $1 * Math. Pow (255) + Regexp. $2 * Math. Pow () + Regexp. $3 * + Regexp. $4*1
}
Else
{
Throw new error ("not a correct IP address! ")
}
}

However, if the above program does not use regular expressions, it may be easier to directly use the split function to separate them. The program is as follows:

VaR IP = "10.100.0000168"
IP = IP. Split (".")
Alert ("the IP value is: "+ (IP [0] * 255*255*255 + IP [1] * 255*255 + IP [2] * 255 + IP [3] * 1 ))

(? <=>) [^>] * (? = <)

You can reference this article through this link: http://gubo85.bokee.com/viewdiary.15597407.html

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.