|
| 1 | +const std = @import("std"); |
| 2 | + |
| 3 | +// tools regex |
| 4 | +const creg = @import("match"); |
| 5 | +// tools fluent regex |
| 6 | +const freg = @import("fluent"); |
| 7 | + |
| 8 | +const allocatorPrint = std.heap.page_allocator; |
| 9 | + |
| 10 | +fn isMatch(vbuf : [] const u8 , comptime vmatch : [] const u8) bool { |
| 11 | + |
| 12 | + var rep = freg.match(vmatch,vbuf) ; |
| 13 | + |
| 14 | + while (rep.next()) |value| { |
| 15 | + |
| 16 | + // std.debug.print("Fluent Macth {s}: {s} \r\n",.{vbuf, value}) ; |
| 17 | + |
| 18 | + if ( std.mem.eql(u8, vbuf, value) ) return true else return false ; |
| 19 | + } |
| 20 | + |
| 21 | + return false; |
| 22 | +} |
| 23 | + |
| 24 | + |
| 25 | +pub fn main() !void { |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | + std.debug.print("-----------------------\r\n",.{}); |
| 31 | + var buf : [] const u8 = "P1"; |
| 32 | + // contrôl "P1" |
| 33 | + std.debug.print("Macth {s}: {} \r\n",.{buf,creg.isMatch(buf,"^[A-Z]{1,1}[a-zA-Z0-9]{0,}$")}) ; |
| 34 | + |
| 35 | + std.debug.print("fluent {s} :{} \r\n",.{buf, |
| 36 | + isMatch(buf,"^[A-Z]{1}[a-zA-Z0-9]")}); |
| 37 | + |
| 38 | + |
| 39 | + |
| 40 | + std.debug.print("-----------------------\r\n",.{}); |
| 41 | + buf =undefined; |
| 42 | + buf = "p1"; |
| 43 | + // contrôl "P1" |
| 44 | + std.debug.print("Macth {s}: {} \r\n",.{buf,creg.isMatch(buf,"^[A-Z]{1,1}[a-zA-Z0-9]{0,}$")}) ; |
| 45 | + |
| 46 | + std.debug.print("fluent {s} :{} \r\n",.{buf, |
| 47 | + isMatch(buf,"[A-Z]{1}[a-zA-Z0-9]{1,2}") }); |
| 48 | + |
| 49 | + |
| 50 | + std.debug.print("-----------------------\r\n",.{}); |
| 51 | + buf =undefined; |
| 52 | + buf = "P"; |
| 53 | + // contrôl obligatoire P and 1 |
| 54 | + std.debug.print("Macth {s}: {} \r\n",.{buf,creg.isMatch(buf,"^[A-Z]{1,1}[a-zA-Z0-9]{1,}$")}) ; |
| 55 | + |
| 56 | + std.debug.print("fluent {s} :{} \r\n",.{buf, |
| 57 | + isMatch(buf,"[A-Z]{1}[a-zA-Z0-9]{1,2}") |
| 58 | + }); |
| 59 | + |
| 60 | + |
| 61 | + std.debug.print("-----------------------\r\n",.{}); |
| 62 | + buf =undefined; |
| 63 | + buf = "P1abc"; |
| 64 | + std.debug.print("Macth {s}: {} \r\n",.{buf,creg.isMatch(buf,"^[A-Z]{1,1}[a-zA-Z0-9]{0,}$")}) ; |
| 65 | + // Attenion length |
| 66 | + std.debug.print("fluent {s} :{} \r\n",.{buf, |
| 67 | + isMatch(buf,"[A-Z]{1}[a-zA-Z0-9]{1,5}") |
| 68 | + }); |
| 69 | + |
| 70 | + // not accept var |
| 71 | + // const allocRegex = std.heap.page_allocator; |
| 72 | + // const xx = std.fmt.allocPrint(allocRegex,"[A-Z]{1}[a-zA-Z0-9]{{1,{d}}}",.{buf.len}) catch unreachable; |
| 73 | + // std.debug.print("fluent:{} \r\n",.{isMatch(buf,xx)}); |
| 74 | + |
| 75 | + std.debug.print("-----------------------\r\n",.{}); |
| 76 | + buf =undefined; |
| 77 | + buf = "P1@"; |
| 78 | + // improper |
| 79 | + std.debug.print("Macth {s}: {} \r\n",.{buf,creg.isMatch(buf,"^[A-Z]{1,1}[a-zA-Z0-9]{0,}$")}) ; |
| 80 | + std.debug.print("fluent {s} :{} \r\n",.{buf, |
| 81 | + isMatch(buf,"[A-Z]{1}[a-zA-Z0-9]{1,3}") |
| 82 | + }); |
| 83 | + |
| 84 | + std.debug.print("-----------------------\r\n",.{}); |
| 85 | + buf =undefined; |
| 86 | + buf = "@P1"; |
| 87 | + // improper |
| 88 | + std.debug.print("Macth {s}: {} \r\n",.{buf,creg.isMatch(buf,"^[A-Z]{1,1}[a-zA-Z0-9]{0,}$")}) ; |
| 89 | + std.debug.print("fluent {s} :{} \r\n",.{buf, |
| 90 | + isMatch(buf,"[A-Z]{1}[a-zA-Z0-9]{1,3}") |
| 91 | + }); |
| 92 | + |
| 93 | + std.debug.print("-----------------------\r\n",.{}); |
| 94 | + buf =undefined; |
| 95 | + buf = "1951-02-29"; |
| 96 | + |
| 97 | + // test date réel contrôl full |
| 98 | + std.debug.print("Macth {s} date iso{} \r\n",.{buf,creg.isMatch( |
| 99 | + buf, |
| 100 | + "^([0-9]{4}[-/]?((0[13-9]|1[012])[-/]?(0[1-9]|[12][0-9]|30)|(0[13578]|1[02])[-/]?31|02[-/]?(0[1-9]|1[0-9]|2[0-8]))|([0-9]{2}(([2468][048]|[02468][48])|[13579][26])|([13579][26]|[02468][048]|0[0-9]|1[0-6])00)[-/]?02[-/]?29)$")}); |
| 101 | + |
| 102 | + std.debug.print("fluent {s} date iso:{} \r\n",.{buf, |
| 103 | + isMatch(buf,"^([0-9]{4}[-/]?((0[13-9]|1[012])[-/]?(0[1-9]|[12][0-9]|30)|(0[13578]|1[02])[-/]?31|02[-/]?(0[1-9]|1[0-9]|2[0-8]))|([0-9]{2}(([2468][048]|[02468][48])|[13579][26])|([13579][26]|[02468][048]|0[0-9]|1[0-6])00)[-/]?02[-/]?29)$") |
| 104 | + }); |
| 105 | + |
| 106 | + |
| 107 | + buf =undefined; |
| 108 | + buf = "1952年02月29日"; |
| 109 | + std.debug.print("Macth {s} date iso{} \r\n",.{buf,creg.isMatch( |
| 110 | + buf, |
| 111 | + "^([0-9]{4}[-/]?((0[13-9]|1[012])[-/]?(0[1-9]|[12][0-9]|30)|(0[13578]|1[02])[-/]?31|02[-/]?(0[1-9]|1[0-9]|2[0-8]))|([0-9]{2}(([2468][048]|[02468][48])|[13579][26])|([13579][26]|[02468][048]|0[0-9]|1[0-6])00)[-/]?02[-/]?29)$")}); |
| 112 | + |
| 113 | + std.debug.print("fluent {s} :{} \r\n",.{buf, |
| 114 | + isMatch(buf,"^([0-9]{4}[-/]?((0[13-9]|1[012])[-/]?(0[1-9]|[12][0-9]|30)|(0[13578]|1[02])[-/]?31|02[-/]?(0[1-9]|1[0-9]|2[0-8]))|([0-9]{2}(([2468][048]|[02468][48])|[13579][26])|([13579][26]|[02468][048]|0[0-9]|1[0-6])00)[-/]?02[-/]?29)$") |
| 115 | + }); // gets lost in this clues |[3][0-1]{1,2} |
| 116 | + |
| 117 | + |
| 118 | + |
| 119 | + |
| 120 | + //oreilly editor book |
| 121 | + buf =undefined; |
| 122 | + buf = "myname.myfirstname@gmail.com"; |
| 123 | + std.debug.print("Macth {s} mail: {} \r\n",.{buf,creg.isMatch( |
| 124 | + buf, |
| 125 | + "^[a-zA-Z0-9_!#$%&'*+/=?`{|}~^.-]+@[a-zA-Z0-9.-]+$")}); |
| 126 | + |
| 127 | + std.debug.print("fluent {s} mail :{} \r\n",.{buf, |
| 128 | + isMatch(buf,"[a-zA-Z0-9_!#$%&.-]+@[a-zA-Z0-9.-]+") |
| 129 | + }); |
| 130 | + |
| 131 | + |
| 132 | + const width :usize = 5; |
| 133 | + const scal :usize = 2; |
| 134 | + buf =undefined; |
| 135 | + buf = "12345.02"; |
| 136 | + // decimal unsigned scal > 0 |
| 137 | + std.debug.print("Macth decimal unsigned {s} scal > 0 {} \r\n",.{buf,creg.isMatch( |
| 138 | + buf, |
| 139 | + std.fmt.allocPrint(allocatorPrint, |
| 140 | + "^[0-9]{s}1,{d}{s}[.][0-9]{s}{d}{s}$",.{"{",width,"}","{",scal,"}"} |
| 141 | + ) catch unreachable)}); |
| 142 | + |
| 143 | + std.debug.print("fluent {s} :{} \r\n",.{buf, |
| 144 | + isMatch(buf,"^[0-9]{1,5}[.][0-9]{2}$") |
| 145 | + }); |
| 146 | + |
| 147 | + |
| 148 | + |
| 149 | + // norme rFC 2822 preceding 5322 which includes text in front, |
| 150 | + buf =undefined; |
| 151 | + buf = "jpl_myname.myfirstname@gmail.com"; |
| 152 | + |
| 153 | + std.debug.print("Macth Mail:{s} {} \r\n",.{buf,creg.isMatch( |
| 154 | + buf, |
| 155 | + "^[a-zA-Z0-9!#$%&'*+\\/=?^_`{|}~-]+(?:\\.[a-zA-Z0-9!#$%&'*+\\/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$")}); |
| 156 | + |
| 157 | + |
| 158 | +// var itr2 = freg.match("^[a-zA-Z0-9!#$%&'*+\\/=?^_`{|}~-]+(?:\\.[a-zA-Z0-9!#$%&'*+\\/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$",buf); |
| 159 | +// while (itr2.next()) |str|{ |
| 160 | +// std.debug.print("SHOULD NOT SEE THIS |{s}|\n", .{ str });} |
| 161 | + |
| 162 | + |
| 163 | + |
| 164 | +} |
| 165 | + |
0 commit comments