老外写的正则表达式的类

前端之家收集整理的这篇文章主要介绍了老外写的正则表达式的类前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
using System;
using System.Collections.Generic;
System.Text;
System.Text.RegularExpressions;

namespace CoreWebLibrary.Text.RegularExpressions
{
///<summary>
AutilityclasscontainingfrequentlyusedRegularExpressionPatterns
andtwoutilitymethodstoseeifapassedstringconformstothedesignated
pattern.
</summary>

publicclassPatterns
{

conststringEMAIL=@"^([0-9a-zA-Z]+[-._+&])*[0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}$";

stringUS_ZIPCODE=@"^\d{5}$";
stringUS_ZIPCODE_PLUS_FOUR=@"^\d{5}((-|\s)?\d{4})$";
stringUS_ZIPCODE_PLUS_FOUR_OPTIONAL=@"^\d{5}((-|\s)?\d{4})?$";

PermissiveUSTelephoneRegex.Doesnotallowextensions.
</summary><example>Allows:324-234-3433,3242343434,(234)234-234,(234)234-2343
</example>stringUS_TELEPHONE=@"^([\(]{1}[0-9]{3}[\)]{1}[\.||\-]{0,1}|^[0-9]{3}[\.|\-|]?)?[0-9]{3}(\.|\-|)?[0-9]{4}$";

Thismatchesaurlinthegenericformat
scheme://authority/path?query#fragment
Allows:http://www.yahoo.com,https://www.yahoo.com stringURL=@"^(?<Protocol>\w+):\/\/(?<Domain>[\w.]+\/?)\S*$";

Thismatchesanipaddressintheformatxxx-xxx-xxx-xxx
eachgroupofxxxmustbelessthanorequalto255
Allows:123.123.123.123,192.168.1.1
stringIP_ADDRESS=@"^(?<First>2[0-4]\d|25[0-5]|[01]?\d\d?)\.(?<Second>2[0-4]\d|25[0-5]|[01]?\d\d?)\.(?<Third>2[0-4]\d|25[0-5]|[01]?\d\d?)\.(?<Fourth>2[0-4]\d|25[0-5]|[01]?\d\d?)$";

Thismatchesadateintheformatmm/dd/yy
Allows:01/05/05,12/30/99,04/11/05
Doesnotallow:01/05/2000,2/2/02
</example>stringDATE_MM_DD_YY=@"^(1[0-2]|0[1-9])/(([1-2][0-9]|3[0-1]|0[1-9])/\d\d)$";


Thismatchesadateintheformatmm/yy
Allows:01/05,11/05,04/99
Doesnotallow:1/05,13/05,00/05
stringDATE_MM_YY=@"^((0[1-9])|(1[0-2]))\/(\d{2})$";


Thismatchesonlynumbers(nodecimals)
Allows:0,1,123,4232323,1212322
stringIS_NUMBER_ONLY=@"^([1-9]\d*)$|^0$";

Thismatchesanystringwithonlyalphacharactersupperorlowercase(A-Z)
Allows:abc,ABC,abCd,AbCd
Doesnotallow:AC,abc!,(a,b)
stringIS_ALPHA_ONLY=@"^[a-zA-Z]+$";

Thismatchesanystringwithonlyuppercasealphacharacter(A-Z)
stringIS_UPPER_CASE=@"^[A-Z]+$";

Thismatchesanystringwithonlylowercasealphacharacter(A-Z)
stringIS_LOWER_CASE=@"^[a-z]+$";

Ensuresthestringonlycontainsalpha-numericcharacters,and
notpunctuation,spaces,linebreaks,etc.
Allows:ab2c,112ABC,ab23Cd
stringIS_ALPHA_NUMBER_ONLY=@"^[a-zA-Z0-9]+$";

ValidatesUSCurrency.Requires$sign
Allowsforoptionalcommasanddecimal.
Noleadingzeros.
Allows:$100,000or$10000.00or$10.00or$.10or$0or$0.00
Doesnotallow:$0.10or10.00or10,000stringIS_US_CURRENCY=@"^\$(([1-9]\d*|([1-9]\d{0,2}(\,\d{3})*))(\.\d{1,2})?|(\.\d{1,2}))$|^\$[0](.00)?$";

Matchesmajorcreditcardsincluding:Visa(length16,prefix4);
Mastercard(length16,prefix51-55);
DinersClub/CarteBlanche(length14,prefix36,38,or300-305);
Discover(length16,prefix6011);
AmericanExpress(length15,prefix34or37).
Savesthecardtypeasanamedgrouptofacilitatefurthervalidation
againsta"cardtype"checkBoxinaprogram.
All16digitformatsaregrouped4-4-4-4withanoptionalhyphenorspace
betweeneachgroupof4digits.
TheAmericanExpressformatisgrouped4-6-5withanoptionalhyphenorspace
betweeneachgroupofdigits.
Formattingcharactersmustbeconsistant,i.e.iftwogroupsareseparatedbyahyphen,
allgroupsmustbeseparatedbyahyphenforamatchtooccur.
stringCREDIT_CARD=@"^(?:(?<Visa>4\d{3})|(?<Mastercard>5[1-5]\d{2})|(?<Discover>6011)|(?<DinersClub>(?:3[68]\d{2})|(?:30[0-5]\d))|(?<AmericanExpress>3[47]\d{2}))([-]?)(?(DinersClub)(?:\d{6}\1\d{4})|(?(AmericanExpress)(?:\d{6}\1\d{5})|(?:\d{4}\1\d{4}\1\d{4})))$";

Matchessocialsecurityinthefollowingformatxxx-xx-xxxx
wherexisanumber
Allows:123-45-6789,232-432-1212
stringSOCIAL_SECURITY=@"^\d{3}-\d{2}-\d{4}$";

Matchesx,xwherexisaname,spacesareonlyallowedbetweencommaandname
Allows:christophersen,eric;christophersen,eric
Notallowed:christophersen,eric;
stringNAME_COMMA_NAME=@"^[a-zA-Z]+,\s?[a-zA-Z]+$";

privatePatterns()
{
}


Checkstoseeifthepassedinputhasthepassedpattern
<paramname="pattern">Thepatterntouse</param><paramname="input">Theinputtocheck<returns>Trueiftheinputhasthepattern,falSEOtherwise</returns>staticboolHasPattern(stringpattern,255)">stringinput)
{
RegexregEx
=newRegex(pattern);
returnregEx.IsMatch(input);
}


Checksthepassedinputtomakesureithasallthepatternsinthe
passedpatternsarray
<paramname="patterns">ArrayofpatternsStringvaluetocheckTrueiftheinputhasallofthepatterns,falSEOtherwise.boolHasPatterns(string[]patterns,255)">stringinput)
{
for(inti=0;i<patterns.Length;i++)
{
if(Patterns.HasPattern(patterns[i],input)==false)
returnfalse;
}


true;
}

}

猜你在找的正则表达式相关文章