Common regular Common C # regular expression _c# tutorial

Source: Internet
Author: User
Tags html tags numeric value pow
Common C # Regular expressions!

"^\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+)? $"//nonnegative floating-point number (positive float + 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 of 26 English letters
"^[a-z]+$"//A string of 26 uppercase letters
"^[a-z]+$"///a string consisting of 26 lowercase letters
"^[a-za-z0-9]+$"//A string of numbers and 26 English letters
"^\w+$"//A string of numbers, 26 English 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 leap year and February.
^ ((1[6-9]| [2-9]\d) \d{2})-(0?[ 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 Expressions
Picture src[^>]*[^/]. (?: jpg|bmp|gif) (?:\"| \')
Chinese ^ ([\u4e00-\u9fa5]+|[ a-za-z0-9]+) $
Web site "\<a.+?href=['" "] (?!) http\:\/\/) (?! mailto\:) (>foundanchor>[^ ' "" >]+?) [^>]*?\> "

Matching regular expressions for Chinese characters: [\U4E00-\U9FA5]

Match Double-byte characters (including Chinese characters): [^\x00-\xff]

A regular expression that matches a blank row: \n[\s|] *\r

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

Matching a regular expression with a trailing space: (^\s*) | (\s*$) (Trim function like VBScript)

Regular expression matching an email address: \w+ ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) *

A regular expression that matches URL URLs: http://([\w-]+\.) +[\w-]+ (/[\w-/?%&=]*)?
---------------------------------------------------------------------------
Here is an example:

Use regular expressions to restrict the entry of text boxes in a Web page's form:

The regular expression limit can only be entered in Chinese: onkeyup= "value=value.replace (/[^\u4e00-\u9fa5]/g,") "Onbeforepaste=" Clipboarddata.setdata (' Text ', Clipboarddata.getdata (' text '). Replace (/[^\u4e00-\u9fa5]/g, ') "

1. Only full-width characters can be entered with regular expression restrictions: onkeyup= "Value=value.replace (/[^\uff00-\uffff]/g,") "Onbeforepaste=" Clipboarddata.setdata (' Text ', Clipboarddata.getdata (' text '). Replace (/[^\uff00-\uffff]/g, ') "

2. Only numbers can be entered with regular expression restrictions: onkeyup= "Value=value.replace (/[^\d]/g,") "Onbeforepaste=" Clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^\d]/g, ') "

3. Only numbers and English can be entered using regular expression restrictions: onkeyup= "Value=value.replace (/[\w]/g,") "Onbeforepaste=" Clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^\d]/g, ') "

4. Calculate the length of the string (a double-byte character length meter 2,ascii character count 1)

String.prototype.len=function () {return This.replace ([^\x00-\xff]/g, "AA"). Length;}

There is no trim function in 5.javascript like VBScript, we can use this expression to implement the following:

String.prototype.trim = function ()
{
Return This.replace (/(^\s*) | ( \s*$)/g, "");
}

To decompose and transform an IP address using a regular expression:

6. The following is a JavaScript program that uses regular expressions to match an IP address and converts an IP address to a corresponding numeric value:

function IP2V (IP)
{
re=/(\d+) \. (\d+) \. (\d+) \. (\d+)/g//matching the regular expression of the IP address
if (Re.test (IP))
{
Return Regexp.$1*math.pow (255,3)) +regexp.$2*math.pow (255,2)) +regexp.$3*255+regexp.$4*1
}
Else
{
throw new Error ("Not a correct IP address!")
}
}

However, if the above program does not use a regular expression, and the split function directly to decompose may be simpler, the program is as follows:

var ip= "10.100.20.168"
Ip=ip.split (".")
Alert ("IP value is:" + (IP[0]*255*255*255+IP[1]*255*255+IP[2]*255+IP[3]*1))



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

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.