Changes between Version 1 and Version 2 of RegularExpressionsExample
- Timestamp:
- 11/21/06 19:15:24 (17 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
RegularExpressionsExample
v1 v2 11 11 {{{ 12 12 #!d 13 import std.stdio : writefln; 13 14 import std.regexp; 14 15 import std.c.stdio; 15 16 16 b itisalpha(char[] c)17 bool isalpha(char[] c) 17 18 { 18 19 RegExp myRegExp; 19 20 myRegExp = new RegExp("^[a-zA-Z_]+$", ""); 20 return cast(b it) myRegExp.test(c);21 return cast(bool) myRegExp.test(c); 21 22 } 22 23 23 b itisspace(char[] c)24 bool isspace(char[] c) 24 25 { 25 26 /* true if c is whitespace, false otherwise */ 26 27 27 28 RegExp myRegExp = new RegExp("^\\s+$", ""); 28 return cast(b it) myRegExp.test(c);29 return cast(bool) myRegExp.test(c); 29 30 } 30 31 31 b itisdigit(char[] c)32 bool isdigit(char[] c) 32 33 /* true if c is a decimal digit, false otherwise */ 33 34 { 34 35 RegExp myRegExp = new RegExp("^\\d+$", ""); 35 return cast(b it) myRegExp.test(c);36 return cast(bool) myRegExp.test(c); 36 37 } 37 38 38 b itishexdigit(char[] c)39 bool ishexdigit(char[] c) 39 40 /* true if c is a hexadecimal digit, false otherwise */ 40 41 { 45 46 } 46 47 47 b itisoctdigit(char[] c)48 bool isoctdigit(char[] c) 48 49 /* true if c is an octal digit, false otherwise */ 49 50 { 50 51 RegExp myRegExp = new RegExp("^[0-7]+$", ""); 51 return cast(b it) myRegExp.test(c);52 return cast(bool) myRegExp.test(c); 52 53 } 53 54 54 b itissymbol(char[] c)55 bool issymbol(char[] c) 55 56 /* true if c is legal SQL symbol, false otherwise */ 56 57 { 57 58 RegExp myRegExp = new RegExp("^[\\(\\)\\[\\]\\.,;=<>\\+\\-\\*/&\\^]+$", ""); 58 return cast(b it) myRegExp.test(c);59 return cast(bool) myRegExp.test(c); 59 60 } 60 61 63 64 /* compile with the -unittest flag to run these tests */ 64 65 65 printf("Testing functions...\n");66 writefln("Testing functions..."); 66 67 67 68 assert(isalpha("a") && isalpha("A") && !isalpha("9") && isalpha("_") && isalpha("R") && !isalpha("&")); 79 80 assert(isspace(" ") && isspace("\t") && !isspace("o") && !isspace(".") && !isspace("5")); 80 81 81 printf("Functions tested successfully.\n");82 writefln("Functions tested successfully."); 82 83 } 83 84 86 87 /* Compile with the -debug flag for this statement to run. */ 87 88 88 debug printf("Main Program.\n");89 debug writefln("Main Program."); 89 90 } 90 91 }}} 91 92 92 == Source ==93 93 94 || Link || http://www.dsource.org/tutorials/index.php?show_example=58 || 95 || Posted by || jcc7 || 96 || Date/Time || Fri May 14, 2004 10:52 pm || 94 95 == Sample Batch File == 96 97 {{{ 98 @echo off 99 set pgm=RegularExpressionsExample 100 dmd %pgm%.d -unittest -debug 101 %pgm%.exe 102 pause 103 erase %pgm%.obj 104 erase %pgm%.map 105 }}} 106 107 == Console Output == 108 109 (using `unittest` and `debug` switches) 110 111 {{{ 112 Testing functions... 113 Functions tested successfully. 114 Main Program. 115 }}} 116 117 == Testing == 118 119 Tested with DMD 0.174 on Windows 2000. 120 121 == More Information == 122 123 For more information about Regular Expressions in Phobos, see [http://www.digitalmars.com/d/phobos/std_regexp.html std.regexp].