`
isiqi
  • 浏览: 16869336 次
  • 性别: 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;
}

}

分享到:
评论

相关推荐

    RegexTester 正则表达式

    - **Java**:`java.util.regex`包下的`Pattern`和`Matcher`类处理正则表达式。 - **C#**:`System.Text.RegularExpressions`命名空间下的`Regex`类提供正则功能。 正则表达式的学习曲线可能有些陡峭,但掌握好正则...

    好东西,老外用正则表达式写的HTML分离函数

    存成.asp文件,执行,你用ASPHTTP抓内容的时候用这个很爽,当然自己要改进一下了&lt;&#37;Option ExplicitFunction stripHTML(strHTML)‘Strips the HTML tags from strHTML Dim objRegExp, strOutput...“&lt;.+?&gt;...

    js程序中美元符号$是什么

     2、在正则表达式中,它可以匹配结尾 /sa$/.test(string) 匹配string字符串中的sa,比如string=’125sa’则匹配,string=’125sa21’则不匹配 正则表达式很复杂,这里只是简单的说说。 3、由于受prototype.js(老外...

    IsapiRewriter源码

    如果想要使用或进一步了解IsapiRewriter,需要查看这些文件内容,特别是配置文件,因为配置文件中包含了定义URL重写规则的正则表达式。正则表达式是IsapiRewriter的核心,通过它可以定义复杂的匹配模式,实现灵活的...

    批量翻译(可翻译代码)

    翻译的内容你可以自己制定正则表达式 如果是老外写的英文代码 你可以调用这个程序 吧所有的英文注释 翻译成中文的 使用的时候在AppConfig里的client_id写上你的百度翻译开发者账号AppId 如果你没有用我的CasNoTBoI1...

    pspad454b2309.rar

    7. **查找和替换**:具备强大的查找和替换功能,支持正则表达式,便于快速定位和修改文本。 8. **预览功能**:可以实时预览HTML、CSS等文件,方便调整样式和布局。 9. **项目管理**:可以管理多个项目,保持文件的...

    JS运用的经典封装实例

    12. **代码自动高亮**:虽然不是新技术,但对于开发者来说仍然实用,尤其是对于学习正则表达式的人来说,是很好的参考实现。 13. **酷似Flash效果的图片展示**:使用JavaScript实现的图片展示效果极其接近Flash,为...

Global site tag (gtag.js) - Google Analytics