#include <stdio.h>
#include <regex.h>
int
regex_match ( char * pattern, const char * string,
size_t sub_size, char ** sub)
{
regmatch_t match[ sub_size] ;
size_t n;
size_t m;
size_t d;
int status;
regex_t re;
status = regcomp ( & re, pattern, REG_EXTENDED) ;
if ( status != 0 )
{
return ( status) ;
}
status = regexec ( & re, string, sub_size, match, 0 ) ;
if ( status == 0 )
{
for ( n= 0 ; n < sub_size; n++ )
{
for ( d = 0 , m = match[ n] .rm_so ;
m >= 0 && m < match[ 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) ;
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+CiNpbmNsdWRlIDxyZWdleC5oPgoKaW50CnJlZ2V4X21hdGNoIChjaGFyICpwYXR0ZXJuLCBjb25zdCBjaGFyICpzdHJpbmcsCiAgICAgICAgICAgICBzaXplX3Qgc3ViX3NpemUsIGNoYXIgKipzdWIpCnsKICAgIHJlZ21hdGNoX3QgbWF0Y2hbc3ViX3NpemVdOwogICAgc2l6ZV90ICAgICBuOwogICAgc2l6ZV90ICAgICBtOwogICAgc2l6ZV90ICAgICBkOwogICAgaW50ICAgICAgICBzdGF0dXM7CiAgICByZWdleF90ICAgIHJlOwogICAgc3RhdHVzID0gcmVnY29tcCAoJnJlLCBwYXR0ZXJuLCBSRUdfRVhURU5ERUQpOwogICAgaWYgKHN0YXR1cyAhPSAwKQogICAgICB7CiAgICAgICAgcmV0dXJuIChzdGF0dXMpOwogICAgICB9CiAgICBzdGF0dXMgPSByZWdleGVjICgmcmUsIHN0cmluZywgc3ViX3NpemUsIG1hdGNoLCAwKTsKICAgIGlmIChzdGF0dXMgPT0gMCkKICAgICAgewogICAgICAgIGZvciAobj0wOyBuIDwgc3ViX3NpemU7IG4rKykKICAgICAgICAgIHsKICAgICAgICAgICAgZm9yIChkID0gMCwgbSA9IG1hdGNoW25dLnJtX3NvOwogICAgICAgICAgICAgICAgIG0gPj0gMCAmJiBtIDwgbWF0Y2hbbl0ucm1fZW87CiAgICAgICAgICAgICAgICAgbSsrLCBkKyspCiAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgc3ViW25dW2RdID0gc3RyaW5nW21dOwogICAgICAgICAgICAgIH0KICAgICAgICAgICAgc3ViW25dW2RdID0gJ1wwJzsKICAgICAgICAgIH0KICAgICAgfQogICAgcmVnZnJlZSAoJnJlKTsKICAgIHJldHVybiAoc3RhdHVzKTsKfQoKaW50Cm1haW4gKHZvaWQpCnsKICAgIGludCAgIHJlc3VsdDsKICAgIGNoYXIgKnN0cmluZyAgICAgICA9ICJDaWFvIGFtb3JlIG1pbyI7CiAgICBjaGFyICpyZSAgICAgICAgICAgPSAiQ2lhbyAoYW1vKXJlIjsKICAgIGNoYXIgIHN1YjBbMjAwXTsKICAgICAgICAgIHN1YjBbMF0gPSAnXDAnOwogICAgY2hhciAgc3ViMVsyMDBdOwogICAgICAgICAgc3ViMVswXSA9ICdcMCc7CiAgICBjaGFyICpzdWJbXSA9IHtzdWIwLCBzdWIxfTsKICAgIAogICAgcmVzdWx0ID0gcmVnZXhfbWF0Y2ggKHJlLCBzdHJpbmcsIDIsIHN1Yik7CiAgICAKICAgIGlmIChyZXN1bHQgPT0gMCkKICAgICAgewogICAgICAgIHByaW50ZiAoIklsIG1vZGVsbG8gXCIlc1wiIHRyb3ZhIGNvcnJpc3BvbmRlbnphICIsCiAgICAgICAgICAgICAgICByZSk7CiAgICAgICAgcHJpbnRmICgibmVsbGEgc3RyaW5nYSBcIiVzXCIsIHByZWNpc2FtZW50ZSAiLAogICAgICAgICAgICAgICAgc3RyaW5nKTsKICAgICAgICBwcmludGYgKCJuZWxsYSBwb3J6aW9uZSBcIiVzXCIsIG1lbnRyZSBsYSAiLAogICAgICAgICAgICAgICAgc3ViWzBdKTsKICAgICAgICBwcmludGYgKCJzb3R0b3N0cmluZ2EgZXN0cmF0dGEgw6ggXCIlc1wiLlxuIiwKICAgICAgICAgICAgICAgIHN1YlsxXSk7CiAgICAgIH0KICAgIGVsc2UKICAgICAgewogICAgICAgIHByaW50ZiAoIklsIG1vZGVsbG8gXCIlc1wiICIsIHJlKTsKICAgICAgICBwcmludGYgKCJOT04gdHJvdmEgY29ycmlzcG9uZGVuemEgIik7CiAgICAgICAgcHJpbnRmICgibmVsbGEgc3RyaW5nYSBcIiVzXCJcbiIsIHN0cmluZyk7CiAgICAgIH0KICAgIHJldHVybiAwOwp9