#include <stdio.h>
#include <regex.h>
//
#define restrict /**/
//
//
int
regex_match ( char * restrict pattern,
const char * restrict string,
size_t sub_size, char * restrict sub[ restrict] ,
int cflags, int eflags, int verbose)
{
const int msg_size = 200 ;
char msg[ msg_size] ;
//
regmatch_t pmatch[ sub_size] ;
size_t n;
size_t m;
size_t d;
//
int status;
regex_t re;
//
status = regcomp ( & re, pattern, cflags) ;
//
if ( status != 0 )
{
if ( verbose)
{
regerror ( status, & re, msg, msg_size) ;
}
return ( status) ;
}
//
status = regexec ( & re, string, sub_size, pmatch,
eflags) ;
//
if ( status != 0 )
{
if ( verbose)
{
regerror ( status, & re, msg, msg_size) ;
}
}
else
{
for ( n= 0 ; n < sub_size; n++ )
{
for ( d = 0 , m = pmatch[ n] .rm_so ;
m >= 0 && m < pmatch[ n] .rm_eo ;
m++, d++ )
{
sub[ n] [ d] = string[ m] ;
}
sub[ n] [ d] = '\0 ' ;
}
}
//
regfree ( & re) ;
//
return ( status) ;
}
//
//
//
int
main ( void )
{
int result;
char * string = "Ciao amore mio" ;
char * re = "Ciao (amo)re" ;
char sub0[ 200 ] ;
sub0[ 0 ] = '\0 ' ;
char sub1[ 200 ] ;
sub1[ 0 ] = '\0 ' ;
char * sub[ ] = { sub0, sub1} ;
//
result = regex_match ( re, string, 2 , sub, REG_EXTENDED,
0 , 1 ) ;
//
if ( result == 0 )
{
printf ( "Il modello \" %s\" trova corrispondenza " , re) ;
printf ( "nella stringa \" %s\" , precisamente " , string) ;
printf ( "nella porzione \" %s\" , mentre la " , sub[ 0 ] ) ;
printf ( "sottostringa estratta รจ \" %s\" .\n " , sub[ 1 ] ) ;
}
else
{
printf ( "Il modello \" %s\" " , re
) ; printf ( "NON trova corrispondenza " ) ; printf ( "nella stringa \" %s\" \n " , string
) ; }
return 0 ;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxyZWdleC5oPgovLwojZGVmaW5lIHJlc3RyaWN0IC8qKi8KLy8KLy8KaW50CnJlZ2V4X21hdGNoIChjaGFyICpyZXN0cmljdCBwYXR0ZXJuLAogICAgICAgICAgICAgY29uc3QgY2hhciAqcmVzdHJpY3Qgc3RyaW5nLAogICAgICAgICAgICAgc2l6ZV90IHN1Yl9zaXplLCBjaGFyICpyZXN0cmljdCBzdWJbcmVzdHJpY3RdLAogICAgICAgICAgICAgaW50IGNmbGFncywgaW50IGVmbGFncywgaW50IHZlcmJvc2UpCnsKICAgIGNvbnN0IGludCAgbXNnX3NpemUgPSAyMDA7CiAgICBjaGFyICAgICAgIG1zZ1ttc2dfc2l6ZV07CiAgICAvLwogICAgcmVnbWF0Y2hfdCBwbWF0Y2hbc3ViX3NpemVdOwogICAgc2l6ZV90ICAgICBuOwogICAgc2l6ZV90ICAgICBtOwogICAgc2l6ZV90ICAgICBkOwogICAgLy8KICAgIGludCAgICAgICBzdGF0dXM7CiAgICByZWdleF90ICAgcmU7CiAgICAvLwogICAgc3RhdHVzID0gcmVnY29tcCAoJnJlLCBwYXR0ZXJuLCBjZmxhZ3MpOwogICAgLy8KICAgIGlmIChzdGF0dXMgIT0gMCkKICAgICAgewogICAgICAgIGlmICh2ZXJib3NlKQogICAgICAgICAgewogICAgICAgICAgICByZWdlcnJvciAoc3RhdHVzLCAmcmUsIG1zZywgbXNnX3NpemUpOwogICAgICAgICAgICBmcHJpbnRmIChzdGRlcnIsICIlc1xuIiwgbXNnKTsKICAgICAgICAgIH0KICAgICAgICByZXR1cm4gKHN0YXR1cyk7CiAgICAgIH0KICAgIC8vCiAgICBzdGF0dXMgPSByZWdleGVjICgmcmUsIHN0cmluZywgc3ViX3NpemUsIHBtYXRjaCwKICAgICAgICAgICAgICAgICAgICAgIGVmbGFncyk7CiAgICAvLwogICAgaWYgKHN0YXR1cyAhPSAwKQogICAgICB7CiAgICAgICAgaWYgKHZlcmJvc2UpCiAgICAgICAgICB7CiAgICAgICAgICAgIHJlZ2Vycm9yIChzdGF0dXMsICZyZSwgbXNnLCBtc2dfc2l6ZSk7CiAgICAgICAgICAgIGZwcmludGYgKHN0ZGVyciwgIiVzXG4iLCBtc2cpOwogICAgICAgICAgfQogICAgICB9CiAgICBlbHNlCiAgICAgIHsKICAgICAgICBmb3IgKG49MDsgbiA8IHN1Yl9zaXplOyBuKyspCiAgICAgICAgICB7CiAgICAgICAgICAgIGZvciAoZCA9IDAsIG0gPSBwbWF0Y2hbbl0ucm1fc287CiAgICAgICAgICAgICAgICAgbSA+PSAwICYmIG0gPCBwbWF0Y2hbbl0ucm1fZW87CiAgICAgICAgICAgICAgICAgbSsrLCBkKyspCiAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgc3ViW25dW2RdID0gc3RyaW5nW21dOwogICAgICAgICAgICAgIH0KICAgICAgICAgICAgc3ViW25dW2RdID0gJ1wwJzsKICAgICAgICAgIH0KICAgICAgfQogICAgLy8KICAgIHJlZ2ZyZWUgKCZyZSk7CiAgICAvLwogICAgcmV0dXJuIChzdGF0dXMpOwp9Ci8vCi8vCi8vCmludAptYWluICh2b2lkKQp7CiAgICBpbnQgICByZXN1bHQ7CiAgICBjaGFyICpzdHJpbmcgICAgICAgPSAiQ2lhbyBhbW9yZSBtaW8iOwogICAgY2hhciAqcmUgICAgICAgICAgID0gIkNpYW8gKGFtbylyZSI7CiAgICBjaGFyICBzdWIwWzIwMF07CiAgICAgICAgICBzdWIwWzBdID0gJ1wwJzsKICAgIGNoYXIgIHN1YjFbMjAwXTsKICAgICAgICAgIHN1YjFbMF0gPSAnXDAnOwogICAgY2hhciAqc3ViW10gPSB7c3ViMCwgc3ViMX07CiAgICAvLwogICAgcmVzdWx0ID0gcmVnZXhfbWF0Y2ggKHJlLCBzdHJpbmcsIDIsIHN1YiwgUkVHX0VYVEVOREVELAogICAgICAgICAgICAgICAgICAgICAgICAgIDAsIDEpOwogICAgLy8KICAgIGlmIChyZXN1bHQgPT0gMCkKICAgICAgewogICAgICAgIHByaW50ZiAoIklsIG1vZGVsbG8gXCIlc1wiIHRyb3ZhIGNvcnJpc3BvbmRlbnphICIsCiAgICAgICAgICAgICAgICByZSk7CiAgICAgICAgcHJpbnRmICgibmVsbGEgc3RyaW5nYSBcIiVzXCIsIHByZWNpc2FtZW50ZSAiLAogICAgICAgICAgICAgICAgc3RyaW5nKTsKICAgICAgICBwcmludGYgKCJuZWxsYSBwb3J6aW9uZSBcIiVzXCIsIG1lbnRyZSBsYSAiLAogICAgICAgICAgICAgICAgc3ViWzBdKTsKICAgICAgICBwcmludGYgKCJzb3R0b3N0cmluZ2EgZXN0cmF0dGEgw6ggXCIlc1wiLlxuIiwKICAgICAgICAgICAgICAgIHN1YlsxXSk7CiAgICAgIH0KICAgIGVsc2UKICAgICAgewogICAgICAgIHByaW50ZiAoIklsIG1vZGVsbG8gXCIlc1wiICIsIHJlKTsKICAgICAgICBwcmludGYgKCJOT04gdHJvdmEgY29ycmlzcG9uZGVuemEgIik7CiAgICAgICAgcHJpbnRmICgibmVsbGEgc3RyaW5nYSBcIiVzXCJcbiIsIHN0cmluZyk7CiAgICAgIH0KICAgIHJldHVybiAwOwp9