`
ideage
  • 浏览: 318011 次
社区版块
存档分类
最新评论

D语言的正则表达式例子

阅读更多
部分内容来自wiki4D.
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.");

}



分享到:
评论

相关推荐

    正则表达式

    JavaScript采用的是Perl语言正则表达式语法的一个相当完整的子集. 正则表达式的模式规范是由一系列字符构成的.大多数字符(包括所有字母数字字符)描述的都是按照字面意思进行匹配的字符.这样说来,正则表达式/java/...

    正则表达式30分钟入门教程

    学习正则表达式的最好方法是从例子开始,理解例子之后再自己对例子进行修改,实验。下面给出了不少简单的例子,并对它们作了详细的说明。 假设你在一篇英文小说里查找hi,你可以使用正则表达式hi。 这几乎是最简单的...

    js中的正则表达式入门(大量实例代码)

    什么是正则表达式呢? 正则表达式(regular expression)描述了一种字符串匹配的模式,可以用来检查一个字符串是否含有某种子串、将匹配的子串做替换或者从某个字符串中取出符合某个条件的子串等。 先科普一下基本的...

    仅1个例子轻松学习正则表达式

    本文所举例子是在《JavaScript语言精粹》上看到的,看完之后对正则表达式有了进一步的理解,故分享之。 例子 //这是一个用来匹配URL的正则表达式,分组获取不同部分的信息 var parse_url = /^(?:([A-Za-z]+):)?(\/...

    Python_正则表达式:Day16

    正则表达式是一种用来匹配字符串的强有力的武器,它用一种描述性的语言来给字符串定义一个规则,凡是符合规则的字符串,我们就认为它“匹配”了,否则,该字符串就是不合法的。 用\d可以匹配一个数字,\w可以匹配一...

    一个java正则表达式工具类源代码.zip(内含Regexp.java文件)

    * Summary of regular-expression constructs 正则表达式结构简介: * Construct Matches * Characters 字符: * x The character x x 字符 x * \\ The ...

    正则测试工具V1.1.40 vb用户可以在窗体上快速左键三级直接生成vb代码

    正则表达式学习请打开文件夹里的“Microsoft Windows 脚本技术.chm”,依次展开树“Windows 脚本技术 - VBScript - 正则表达式简介”,认真学习吧,需要耐心和不停的试验,学好了绝对是一把利器。我以前学习都要写vb...

    Java-PHP-C#

    此外,JavaScript这种客户端的脚本语言也提供了对正则表达式的支持,现在正则表达式已经成为了一个通用的概念和工具,被各类技术人员所广泛使用。 在某个Linux网站上面有这样的话:"如果你问一下Linux爱好者最喜欢...

    《JavaScript语言精粹[修订版]》高清版_2012.09_【蝴蝶书】_172页完整版

    作者从语法、对象、函数、继承、数组、正则表达式、方法、样式和优美的特性这9 个方面来呈现这门语言真正的精华部分,通过它们完全可以构建出优雅高效的代码。作者还通过附录列出了这门语言的毒瘤和糟粕部分,且告诉...

    Perl实例精解(第四版).文字版_第一部分.zip

    从正则表达式处理到格式化报表,从数据库到进程间通信以及Web开发。它教大学Perl,同时教了大家许多Unix和Windows知识。设计网络、系统调用、IPC和CGI这些主题可以节省了解函数作用、所需要的库以及正确语法的时间。...

    JavaScript语言精粹.pdf

    前言 第1章 精华 ...第7章 正则表达式 7.1 一个例子 7.2 结构 7.3 元素 第8章 方法 第9章 代码风格 第10章 优美的特性 附录A:糟粕 附录B:鸡肋 附录C:JSLint 附录D:语法图 附录E:JSON 索引

    pyautomata:有限自动机的Python模块

    从正则表达式构建FA 在确定NFA的同时改善对新州的管理 安装 目前尚无车轮包装。 您可以使用setuptools从github直接安装它。 pip install git+https://github.com/pjdevs/pyautomata 例子 a = NFAutomaton ( set ( ...

    js使用小技巧

    Javascript小技巧一箩筐 ...语言设置 onclick="window.external.ShowBrowserUI("LanguageDialog", null)"&gt; 加入收藏夹 onclick="window.external.AddFavorite("http://www.google.com/", "google...

    PHP3程序设计

    9.1 正则表达式定义 129 9.1.1 方括号表达式 130 9.1.2 转义字符 130 9.2 POSIX风格的函数 131 9.2.1 ereg 和eregi 131 9.2.2 ereg_replace 和eregi_replace 132 9.2.3 Split 133 9.3 PERL风格函数 134 9.3.1 模式定...

    RED HAT LINUX 6大全

    9.6.2 /etc/rc.d httpd脚本 168 9.7 配置文件清单 170 9.8 小结 185 第10章 Internet新闻 186 10.1 Linux与新闻组 186 10.1.1 新闻供给点如何工作 187 10.1.2 推/拉新闻 187 10.1.3 下载新闻组的可选方法 187 10.2 ...

    精通Qt4编程(第二版)源代码

    \13.5.1 基本的正则表达式 342 \13.5.2 文字捕获 344 \13.6 小结 345 \高 级 篇 \第14章 XML 348 \14.1 DOM 348 \14.1.1 DOM入门 348 \14.1.2 使用DOM 348 \14.1.3 使用DOM写XML文件 352 \14.2 SAX 354 \...

    精通qt4编程(源代码)

    \13.5.1 基本的正则表达式 342 \13.5.2 文字捕获 344 \13.6 小结 345 \高 级 篇 \第14章 XML 348 \14.1 DOM 348 \14.1.1 DOM入门 348 \14.1.2 使用DOM 348 \14.1.3 使用DOM写XML文件 352 \14.2 SAX 354 \14.3 基于流...

Global site tag (gtag.js) - Google Analytics