Regular Expressions
Part of StandardLibraryCategory
Description
Demonstrates some simple uses of regular expressions. These examples are a little contrived as it'd actually be easier to use std.string to do these tests.
Example
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); } 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")); writefln("Functions tested successfully."); } void main() { /* Compile with the -debug flag for this statement to run. */ debug writefln("Main Program."); }
Sample Batch File
@echo off set pgm=RegularExpressionsExample dmd %pgm%.d -unittest -debug %pgm%.exe pause erase %pgm%.obj erase %pgm%.map
Console Output
(using unittest and debug switches)
Testing functions... Functions tested successfully. Main Program.
Testing
Tested with DMD 0.174 on Windows 2000.
More Information
For more information about Regular Expressions in Phobos, see std.regexp.