`
isiqi
  • 浏览: 16135406 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

老外写的正则表达式的类

阅读更多
又是在www.asp.net上找到的.因为其很简单并且作者也是直接了当把代码拿上来了,所以就不翻译了
代码如下

usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
usingSystem.Text.RegularExpressions;

namespaceCoreWebLibrary.Text.RegularExpressions
{
/**////<summary>
///AutilityclasscontainingfrequentlyusedRegularExpressionPatterns
///andtwoutilitymethodstoseeifapassedstringconformstothedesignated
///pattern.
///</summary>

publicclassPatterns
{

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

publicconststringUS_ZIPCODE=@"^\d{5}$";
publicconststringUS_ZIPCODE_PLUS_FOUR=@"^\d{5}((-|\s)?\d{4})$";
publicconststringUS_ZIPCODE_PLUS_FOUR_OPTIONAL=@"^\d{5}((-|\s)?\d{4})?$";

/**////<summary>
///PermissiveUSTelephoneRegex.Doesnotallowextensions.
///</summary>
///<example>
///Allows:324-234-3433,3242343434,(234)234-234,(234)234-2343
///</example>

publicconststringUS_TELEPHONE=@"^([\(]{1}[0-9]{3}[\)]{1}[\.||\-]{0,1}|^[0-9]{3}[\.|\-|]?)?[0-9]{3}(\.|\-|)?[0-9]{4}$";

/**////<summary>
///Thismatchesaurlinthegenericformat
///scheme://authority/path?query#fragment
///</summary>
///<example>
///Allows:http://www.yahoo.com,https://www.yahoo.com,ftp://www.yahoo.com
///</example>

publicconststringURL=@"^(?<Protocol>\w+):\/\/(?<Domain>[\w.]+\/?)\S*$";

/**////<summary>
///Thismatchesanipaddressintheformatxxx-xxx-xxx-xxx
///eachgroupofxxxmustbelessthanorequalto255
///</summary>
///<example>
///Allows:123.123.123.123,192.168.1.1
///</example>

publicconststringIP_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?)$";

/**////<summary>
///Thismatchesadateintheformatmm/dd/yy
///</summary>
///<example>
///Allows:01/05/05,12/30/99,04/11/05
///Doesnotallow:01/05/2000,2/2/02
///</example>

publicconststringDATE_MM_DD_YY=@"^(1[0-2]|0[1-9])/(([1-2][0-9]|3[0-1]|0[1-9])/\d\d)$";


/**////<summary>
///Thismatchesadateintheformatmm/yy
///</summary>
///<example>
///Allows:01/05,11/05,04/99
///Doesnotallow:1/05,13/05,00/05
///</example>

publicconststringDATE_MM_YY=@"^((0[1-9])|(1[0-2]))\/(\d{2})$";


/**////<summary>
///Thismatchesonlynumbers(nodecimals)
///</summary>
///<example>
///Allows:0,1,123,4232323,1212322
///</example>

publicconststringIS_NUMBER_ONLY=@"^([1-9]\d*)$|^0$";

/**////<summary>
///Thismatchesanystringwithonlyalphacharactersupperorlowercase(A-Z)
///</summary>
///<example>
///Allows:abc,ABC,abCd,AbCd
///Doesnotallow:AC,abc!,(a,b)
///</example>

publicconststringIS_ALPHA_ONLY=@"^[a-zA-Z]+$";

/**////<summary>
///Thismatchesanystringwithonlyuppercasealphacharacter(A-Z)
///</summary>

publicconststringIS_UPPER_CASE=@"^[A-Z]+$";

/**////<summary>
///Thismatchesanystringwithonlylowercasealphacharacter(A-Z)
///</summary>

publicconststringIS_LOWER_CASE=@"^[a-z]+$";

/**////<summary>
///Ensuresthestringonlycontainsalpha-numericcharacters,and
///notpunctuation,spaces,linebreaks,etc.
///</summary>
///<example>
///Allows:ab2c,112ABC,ab23Cd
///Doesnotallow:AC,abc!,a.a
///</example>

publicconststringIS_ALPHA_NUMBER_ONLY=@"^[a-zA-Z0-9]+$";

/**////<summary>
///ValidatesUSCurrency.Requires$sign
///Allowsforoptionalcommasanddecimal.
///Noleadingzeros.
///</summary>
///<example>Allows:$100,000or$10000.00or$10.00or$.10or$0or$0.00
///Doesnotallow:$0.10or10.00or10,000</example>

publicconststringIS_US_CURRENCY=@"^\$(([1-9]\d*|([1-9]\d{0,2}(\,\d{3})*))(\.\d{1,2})?|(\.\d{1,2}))$|^\$[0](.00)?$";

/**////<summary>
///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.
///</summary>

publicconststringCREDIT_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})))$";

/**////<summary>
///Matchessocialsecurityinthefollowingformatxxx-xx-xxxx
///wherexisanumber
///</summary>
///<example>
///Allows:123-45-6789,232-432-1212
///</example>

publicconststringSOCIAL_SECURITY=@"^\d{3}-\d{2}-\d{4}$";

/**////<summary>
///Matchesx,xwherexisaname,spacesareonlyallowedbetweencommaandname
///</summary>
///<example>
///Allows:christophersen,eric;christophersen,eric
///Notallowed:christophersen,eric;
///</example>

publicconststringNAME_COMMA_NAME=@"^[a-zA-Z]+,\s?[a-zA-Z]+$";

privatePatterns()
{
}


/**////<summary>
///Checkstoseeifthepassedinputhasthepassedpattern
///</summary>
///<paramname="pattern">Thepatterntouse</param>
///<paramname="input">Theinputtocheck</param>
///<returns>Trueiftheinputhasthepattern,falseotherwise</returns>

publicstaticboolHasPattern(stringpattern,stringinput)
{
RegexregEx
=newRegex(pattern);
returnregEx.IsMatch(input);
}


/**////<summary>
///Checksthepassedinputtomakesureithasallthepatternsinthe
///passedpatternsarray
///</summary>
///<paramname="patterns">Arrayofpatterns</param>
///<paramname="input">Stringvaluetocheck</param>
///<returns>Trueiftheinputhasallofthepatterns,falseotherwise.</returns>

publicstaticboolHasPatterns(string[]patterns,stringinput)
{
for(inti=0;i<patterns.Length;i++)
{
if(Patterns.HasPattern(patterns[i],input)==false)
returnfalse;
}


returntrue;
}

}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics