11const  std  =  @import ("std" );
2- const  re  =  @cImport ({
3-  @cDefine ("PCRE2_CODE_UNIT_WIDTH" , "8" );
4-  @cInclude ("regPcre2.h" );
5- });
6- 7- //------------------------- 
8- // idem ./curse/match.zig 
9- //------------------------- 
10- pub  fn  isMatch (strVal  : [] const  u8 , regVal  : [] const  u8  ) bool  {
11-  const  allocator  =  std .heap .page_allocator ;
12-  var  slice  =  allocator .alignedAlloc (u8 , @sizeOf (usize ),@sizeOf (usize )) catch  unreachable ;
13-  defer  allocator .free (slice );
14- 15-  const  regex : * re.regex_t  =  @ptrCast (slice  );
16-  defer  re .pcre2_regfree (regex ); // IMPORTANT!! 
17- 18-  const  creg : []u8  =  allocator .alloc (u8 , regVal .len  , ) catch  unreachable ;
19-  defer  allocator .free (creg );
20- 21-  std .mem .copy (u8 , creg , regVal );
22- 23-  if  (re .pcre2_regcomp (regex ,@ptrCast (creg ),re .REG_EXTENDED  |  re .REG_ICASE ) !=  0 ) {
24-  // TODO: the pattern is invalid 
25-  // display for test  
26-  // std.debug.print("error patern {s}\n", .{regVal}); 
27-  return  false  ;
28-  }
29- 30-  const  cval : []u8  =  allocator .alloc (u8 , strVal .len  ) catch  unreachable ;
31-  defer  allocator .free (cval );
32- 33-  std .mem .copy (u8 , cval , strVal );
34- 35-  return  re .isMatch (regex , @ptrCast (cval ));
36- }
37- 382
3+ // tools regex 
4+ const  reg  =  @import ("match" );
395
6+ const  allocatorPrint  =  std .heap .page_allocator ;
407
418pub  fn  main () ! void  {
429
10+ const  stdin  =  std .io .getStdIn ().reader ();
11+ var  buf  : [3 ]u8  =  undefined ;
12+ buf  =  [_ ]u8 {0 } **  3 ;
4313
44-  std .debug .print ("Macth abc {} \r \n " ,.{isMatch ("p1" ,"^[a-zA-Z]{1,1}[a-zA-Z0-9]{0,}$" )}) ;
4514
46-  std .debug .print ("Macth digit {} \r \n " ,.{isMatch (
15+  std .debug .print ("Macth abc {} \r \n " ,.{reg .isMatch ("p1" ,"^[a-zA-Z]{1,1}[a-zA-Z0-9]{0,}$" )}) ;
16+ 17+  std .debug .print ("Macth digit {} \r \n " ,.{reg .isMatch (
4718 "423" ,
4819 "^[1-9]{1,1}?[0-9]{0,}$" )}) ;
4920
50-  std .debug .print ("Macth tel fr{} \r \n " ,.{isMatch (
21+  std .debug .print ("Macth tel fr{} \r \n " ,.{reg . isMatch (
5122 "+(33)6.12.34.56.78" ,
5223 "^[+]{1,1}[(]{0,1}[0-9]{1,3}[)]([0-9]{1,3}){1,1}([-. ]?[0-9]{2,3}){2,4}$" )}) ;
5324
54-  std .debug .print ("Macth tel us{} \r \n " ,.{isMatch (
55-  "+(001)456.123.089 " ,
25+  std .debug .print ("Macth tel us{} \r \n " ,.{reg . isMatch (
26+  "+(001)456.123.789 " ,
5627 "^[+]{1,1}[(]([0-9]{3,3})[)]([-. ]?[0-9]{3}){2,4}$" )}) ;
5728
58-  std .debug .print ("Macth date fr{} \r \n " ,.{isMatch (
29+  std .debug .print ("Macth date fr{} \r \n " ,.{reg . isMatch (
5930 "12/10/1951" ,
6031 "^(0[1-9]|[12][0-9]|3[01])[\\ /](0[1-9]|1[012])[\\ /][0-9]{4,4}$" )});
6132
6233
63-  std .debug .print ("Macth date us{} \r \n " ,.{isMatch (
34+  std .debug .print ("Macth date us{} \r \n " ,.{reg . isMatch (
6435 "10/12/1951" ,
6536 "^(0[1-9]|1[012])[\\ /](0[1-9]|[12][0-9]|3[01])[\\ /][0-9]{4,4}$" )});
6637
67-  std .debug .print ("Macth date iso{} \r \n " ,.{isMatch (
68-  "1951年10月12日 " ,
38+  std .debug .print ("Macth date iso{} \r \n " ,.{reg . isMatch (
39+  "2003年02月25日 " ,
6940 "^([0-9]{4,4})[-](0[1-9]|1[012])[-](0[1-9]|[12][0-9]|3[01])$" )});
7041
7142
7243 // https://stackoverflow.com/questions/201323/how-can-i-validate-an-email-address-using-a-regular-expression 
7344 // chapitre RFC 6532 updates 5322 to allow and include full, clean UTF-8. 
7445
75-  std .debug .print ("Macth Mail{} \r \n " ,.{isMatch (
46+  std .debug .print ("Macth Mail{} \r \n " ,.{reg . isMatch (
7647 "myname.myfirstname@gmail.com" ,
7748 "^([-!#-\' *+\\ /-9=?A-Z^-~]{1,64}(\\ .[-!#-\' *+\\ /-9=?A-Z^-~]{1,64})*|\" ([]!#-[^-~ \t ]|(\\ [\t  -~]))+\" )@[0-9A-Za-z]([0-9A-Za-z-]{0,61}[0-9A-Za-z])?(\\ .[0-9A-Za-z]([0-9A-Za-z-]{0,61}[0-9A-Za-z])?)+$" )});
7849
7950 //oreilly 
80-  std .debug .print ("Macth Mail{} \r \n " ,.{isMatch (
51+  std .debug .print ("Macth Mail{} \r \n " ,.{reg . isMatch (
8152 "myname.myfirstname@gmail.com" ,
8253 "^[A-Z0-9_!#$%&'*+/=?`{|}~^.-]+@[A-Z0-9.-]+$" )});
8354
8455
85-  var  arenaPrint =  std .heap .ArenaAllocator .init (std .heap .page_allocator );
86-  var  allocatorPrint  =  arenaPrint .allocator ();
8756
8857
8958 var  width  :usize  =  5 ;
9059 // unsigned digit 
91-  std .debug .print ("Macth digit unsigned{} \r \n " ,.{isMatch (
60+  std .debug .print ("Macth digit unsigned{} \r \n " ,.{reg . isMatch (
9261 "123" ,
9362 std .fmt .allocPrint (allocatorPrint ,"^[0-9]{s}{d}{s}$" ,.{"{1," ,width ,"}" },) catch  unreachable )});
9463
9564 // unsigned digit 
96-  std .debug .print ("Macth digit {} \r \n " ,.{isMatch (
65+  std .debug .print ("Macth digit {} \r \n " ,.{reg . isMatch (
9766 "+12345" ,
9867 std .fmt .allocPrint (allocatorPrint ,"^[+-][0-9]{s}{d}{s}$" ,.{"{1," ,width ,"}" },) catch  unreachable )});
9968
10069
10170 // decimal unsigned scal = 0 
102-  std .debug .print ("Macth decimal unsigned scal = 0 {} \r \n " ,.{isMatch (
71+  std .debug .print ("Macth decimal unsigned scal = 0 {} \r \n " ,.{reg . isMatch (
10372 "12345" ,
10473 std .fmt .allocPrint (allocatorPrint ,"^[0-9]{s}1,{d}{s}$" ,.{"{" ,width ,"}" },) catch  unreachable )});
10574
10675 var  scal  :usize  =  2 ;
10776 // decimal unsigned scal > 0 
108-  std .debug .print ("Macth decimal unsigned scal > 0 {} \r \n " ,.{isMatch (
77+  std .debug .print ("Macth decimal unsigned scal > 0 {} \r \n " ,.{reg . isMatch (
10978 "12345.02" ,
11079 std .fmt .allocPrint (allocatorPrint ,
11180 "^[0-9]{s}1,{d}{s}[.][0-9]{s}{d}{s}$" ,.{"{" ,width ,"}" ,"{" ,scal ,"}" }
11281 ) catch  unreachable )});
11382
114- 83+ _ = try stdin . readUntilDelimiterOrEof ( buf [0 .. ],  ' \n ' ); 
11584
11685 // decimal signed scal = 0 
117-  std .debug .print ("Macth decimal signed scal = 0 {} \r \n " ,.{isMatch (
86+  std .debug .print ("Macth decimal signed scal = 0 {} \r \n " ,.{reg . isMatch (
11887 "+12345" ,
11988 std .fmt .allocPrint (allocatorPrint ,"^[+-][0-9]{s}1,{d}{s}$" ,.{"{" ,width ,"}" },) catch  unreachable )});
12089
12190
12291 // decimal unsigned scal > 0 
123-  std .debug .print ("Macth decimal signed scal > 0 {} \r \n " ,.{isMatch (
92+  std .debug .print ("Macth decimal signed scal > 0 {} \r \n " ,.{reg . isMatch (
12493 "+12345.02" ,
12594 std .fmt .allocPrint (allocatorPrint ,
12695 "^[+-][0-9]{s}1,{d}{s}[.][0-9]{s}{d}{s}$" ,.{"{" ,width ,"}" ,"{" ,scal ,"}" }
12796 ) catch  unreachable )});
12897
12998 var  i : usize  =  0  ;
13099 while (i  <  5000 ) : ( i  +=  1  ) {
131-  _ = isMatch (
100+  _ = reg . isMatch (
132101 "1951年10月12日" ,
133102 "^([0-9]{4,4})[-](0[1-9]|1[012])[-](0[1-9]|[12][0-9]|3[01])$" );
134103 }
135104
136- arenaPrint .deinit ();
137105
138- const  stdin  =  std .io .getStdIn ().reader ();
139- var  buf :[4 ]u8  =  [_ ]u8 {0 } **  4 ;
106+ buf  =  [_ ]u8 {0 } **  3 ;
140107 _ =  try  stdin .readUntilDelimiterOrEof (buf [0.. ], '\n ' );
141108}
0 commit comments