D语言中国主页  D语言编辑器SciTE4D   DWin库 D语言官方网站                                             
D语言编译器1.x最新版 OpenSource   Tango   webnews Wiki                                             

查看完整版本: D语言的正则表达式例子

yidabu 2007-4-27 20:01

D语言的正则表达式例子

D语言的正则表达式例子知识若不分享 实在没有意义 http://www.d-programming-language-china.org 20070427点击下面网址查看原文:http://www.d-programming-language-china.org        关键字:        正则表达式        部分内容来自wiki4D.        by:        ideage        from:        http://ideage.javaeye.com/blog/59738        [Copy to clipboard] [ - ]CODE:                                module regexp;                                import std.stdio : writefln;                import std.regexp;                import std.c.stdio;                                bool isalpha(char[] c)                {                    RegExp myRegExp;                    myRegExp = new RegExp("^[a-zA-Z_]+$", "");                    return cast(bool) myRegExp.test(c);                }                                bool isspace(char[] c)                {                     /* true if c is whitespace, false otherwise */                                    RegExp myRegExp = new RegExp("^\\s+$", "");                    return cast(bool) myRegExp.test(c);                }                                bool isdigit(char[] c)                /* true if c is a decimal digit, false otherwise */                {                    RegExp myRegExp = new RegExp("^\\d+$", "");                    return cast(bool) myRegExp.test(c);                }                                bool ishexdigit(char[] c)                /* true if c is a hexadecimal digit, false otherwise */                {                    RegExp myRegExp = new RegExp("^[0-9A-F]+$", "");                    /* If it were D code, "_" would also be valid */                                    return cast(bit) myRegExp.test(c);                }                                bool isoctdigit(char[] c)                /* true if c is an octal digit, false otherwise */                {                    RegExp myRegExp = new RegExp("^[0-7]+$", "");                    return cast(bool) myRegExp.test(c);                }                                bool issymbol(char[] c)                /* true if c is legal SQL symbol, false otherwise */                {                    RegExp myRegExp = new RegExp("^[\\(\\)\\[\\]\\.,;=<>\\+\\-\\*/&\\^]+$", "");                    return cast(bool) myRegExp.test(c);                }                                bool isDate(char[] c)                /* true if c is a date, false otherwise */                {                    RegExp myRegExp = new RegExp("((((19){1}|(20){1})d{2})|d{2})[01]{1}d{1}[0-3]{1}d{1}", ""); //1900                    return cast(bool) myRegExp.test(c);                }                                bool isChinese(char[] c)                /* true if c is a chinese string, false otherwise */                {                    RegExp myRegExp = new RegExp("^[\u4e00-\u9fa5]+$", "");                    return cast(bool) myRegExp.test(c);                }                                bool iscnPhone(char[] c)                /* true if c is a china phone code, false otherwise */                {                    RegExp myRegExp = new RegExp("\\d{3}-\\d{8}|\\d{4}-\\d{7}", "g");                    return cast(bool) myRegExp.test(c);                }                                bool iscnMobile(char[] c)                /* true if c is a china Mobile code, false otherwise */                {                    RegExp myRegExp = new RegExp("^((\\(\\d{2,3}\\))|(\\d{3}\\-))?13\\d{9}$", "");                    return cast(bool) myRegExp.test(c);                }                                bool iscnZip(char[] c)                /* true if c is a china ZIP, false otherwise */                {                    RegExp myRegExp = new RegExp("^[0-9]\\d{5}$", "");                    return cast(bool) myRegExp.test(c);                }                                bool iscnIDcard(char[] c)                /* true if c is a china ID card, false otherwise */                {                    RegExp myRegExp = new RegExp("\\d{15}|\\d{18}", "");                    return cast(bool) myRegExp.test(c);                }                                unittest                {                    /* compile with the -unittest flag to run these tests */                                    writefln("Testing functions...");                                    assert(isalpha("a") && isalpha("A") && !isalpha("9") && isalpha("_") && isalpha("R") && !isalpha("&"));                                    assert(issymbol("(") && issymbol(")") && issymbol("[") && issymbol("]") && issymbol(")") &&                        issymbol("[") && issymbol("]") && issymbol("-") && issymbol("/") && issymbol("=") && issymbol("*") &&                        issymbol(".") && !issymbol("a") && !issymbol("0") && !issymbol("Y") && !issymbol("\\"));                                    assert(isdigit("0") && isdigit("7") && isdigit("9") && !isdigit("A")    && !isdigit("^") && !isdigit("G"));                                    assert(ishexdigit("0") && ishexdigit("7") && ishexdigit("A")    && !ishexdigit("^") && !ishexdigit("G"));                                    assert(isoctdigit("0") && isoctdigit("7") && !isoctdigit("8")    && !isoctdigit("A")    && !isoctdigit("^"));                                    assert(isspace(" ")    && isspace("\t") && !isspace("o")    && !isspace(".")    && !isspace("5"));                                    assert(isChinese("中文")    && isChinese("哦") && !isChinese("*.")    && !isChinese("abcd")    && !isChinese("5"));                                        assert(iscnPhone("010-12345678")    && iscnPhone("0710-1234567") && !iscnPhone("01-12345")    && !iscnPhone("010-12")    && !iscnPhone("0314-123456") && iscnPhone("0314-12345678-90")&& iscnPhone("0314-12345678-901") && iscnPhone("012345-12345678-901") );                                        assert(iscnMobile("13123456789")&& !iscnMobile("139123456789") && !iscnMobile("*.")    && !iscnMobile("abcd")    && !iscnMobile("5")    );                                        assert(iscnZip("100081")&& iscnZip("012346") && !iscnZip("*.")    && !iscnZip("abcd")    && !iscnZip("5")    );                                    writefln("Functions tested successfully.");                }                                void main()                {                    /* Compile with the -debug flag for this statement to run. */                                    debug writefln("Main Program.");                                }        ( lastupdate:20070427 最新文章请访问http://www.d-programming-language-china.org )关于一大步成功社区:yidabu提倡在交流中学习,在分享中提高收集感兴趣的知识,写下心得,通过网络与别人一起分享理解一点就实践一步,收获什么就分享什么,成功就是这样一点点一步步累积起来的网络只是一个工具,只有自己身心提高才是实实在在的。d-programming-language-china.org为大家提供一个学习交流各种知识的平台
页: [1]
查看完整版本: D语言的正则表达式例子