1 module tern.regex; 2 3 import tern.regex.builder; 4 import tern.regex.matcher; 5 6 package static string[string] lookups; 7 static this() 8 { 9 lookups["\\w"] = expand("a-zA-Z0-9_", lookups); 10 lookups["\\d"] = expand("0-9", lookups); 11 lookups["\\s"] = expand(" t\r\n\f", lookups); 12 lookups["\\h"] = expand(" t", lookups); 13 lookups["\\t"] = expand("\t", lookups); 14 lookups["\\r"] = expand("\r", lookups); 15 lookups["\\n"] = expand("\n", lookups); 16 lookups["\\f"] = expand("\f", lookups); 17 lookups["\\v"] = expand("\v", lookups); 18 lookups["\\b"] = expand("\b", lookups); 19 lookups["\\a"] = expand("\a", lookups); 20 lookups["\\0"] = expand("\0", lookups); 21 } 22 23 public template regex(string PATTERN, ubyte FLAGS = GLOBAL) 24 { 25 public: 26 final: 27 Regex ctor() 28 { 29 return new Regex(PATTERN, FLAGS); 30 } 31 32 pure string[] matchFirst(string TEXT)() 33 { 34 string[string] lookups; 35 lookups["\\w"] = expand("a-zA-Z0-9_", lookups); 36 lookups["\\d"] = expand("0-9", lookups); 37 lookups["\\s"] = expand(" t\r\n\f", lookups); 38 lookups["\\h"] = expand(" t", lookups); 39 lookups["\\t"] = expand("\t", lookups); 40 lookups["\\r"] = expand("\r", lookups); 41 lookups["\\n"] = expand("\n", lookups); 42 lookups["\\f"] = expand("\f", lookups); 43 lookups["\\v"] = expand("\v", lookups); 44 lookups["\\b"] = expand("\b", lookups); 45 lookups["\\a"] = expand("\a", lookups); 46 lookups["\\0"] = expand("\0", lookups); 47 48 auto ret = matchInternal(PATTERN.build(lookups), FLAGS, TEXT, 1); 49 return ret.length != 0 ? ret[0] : null; 50 } 51 52 pure string[][] match(string TEXT)() 53 { 54 string[string] lookups; 55 lookups["\\w"] = expand("a-zA-Z0-9_", lookups); 56 lookups["\\d"] = expand("0-9", lookups); 57 lookups["\\s"] = expand(" t\r\n\f", lookups); 58 lookups["\\h"] = expand(" t", lookups); 59 lookups["\\t"] = expand("\t", lookups); 60 lookups["\\r"] = expand("\r", lookups); 61 lookups["\\n"] = expand("\n", lookups); 62 lookups["\\f"] = expand("\f", lookups); 63 lookups["\\v"] = expand("\v", lookups); 64 lookups["\\b"] = expand("\b", lookups); 65 lookups["\\a"] = expand("\a", lookups); 66 lookups["\\0"] = expand("\0", lookups); 67 68 return matchInternal(PATTERN.build(lookups), FLAGS, TEXT); 69 } 70 } 71 72 public class Regex 73 { 74 private: 75 final: 76 Element[] elements; 77 ubyte flags; 78 79 public: 80 81 this(string pattern, ubyte flags = GLOBAL) 82 { 83 this.elements = pattern.build(lookups); 84 this.flags = flags; 85 } 86 87 string[] matchFirst(string text) 88 { 89 auto ret = matchInternal(elements, flags, text, 1); 90 return ret.length != 0 ? ret[0] : null; 91 } 92 93 string[][] match(string text) 94 { 95 return matchInternal(elements, flags, text); 96 } 97 }