#include <stdio.h>
#include <regex.h>
int
regex_match (char *pattern, const char *string,
size_t sub_size, char **sub)
{
const int msg_size = 200;
char msg[msg_size];
msg[0] = '\0';
//
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)
{
regerror (status, &re, msg, msg_size);
return (status);
}
status = regexec (&re, string, sub_size, match, 0);
if (status != 0)
{
regerror (status, &re, msg, msg_size);
}
else
{
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+CiNpbmNsdWRlIDxyZWdleC5oPgoKaW50CnJlZ2V4X21hdGNoIChjaGFyICpwYXR0ZXJuLCBjb25zdCBjaGFyICpzdHJpbmcsCiAgICAgICAgICAgICBzaXplX3Qgc3ViX3NpemUsIGNoYXIgKipzdWIpCnsKICAgIGNvbnN0IGludCBtc2dfc2l6ZSA9IDIwMDsKICAgIGNoYXIgICAgICBtc2dbbXNnX3NpemVdOwogICAgICAgICAgICAgIG1zZ1swXSA9ICdcMCc7CiAgICAvLwogICAgcmVnbWF0Y2hfdCBtYXRjaFtzdWJfc2l6ZV07CiAgICBzaXplX3QgICAgIG47CiAgICBzaXplX3QgICAgIG07CiAgICBzaXplX3QgICAgIGQ7CiAgICBpbnQgICAgICAgIHN0YXR1czsKICAgIHJlZ2V4X3QgICAgcmU7CiAgICBzdGF0dXMgPSByZWdjb21wICgmcmUsIHBhdHRlcm4sIFJFR19FWFRFTkRFRCk7CiAgICBpZiAoc3RhdHVzICE9IDApCiAgICAgIHsKICAgICAgICByZWdlcnJvciAoc3RhdHVzLCAmcmUsIG1zZywgbXNnX3NpemUpOwogICAgICAgIGZwcmludGYgKHN0ZGVyciwgIiVzXG4iLCBtc2cpOwogICAgICAgIHJldHVybiAoc3RhdHVzKTsKICAgICAgfQogICAgc3RhdHVzID0gcmVnZXhlYyAoJnJlLCBzdHJpbmcsIHN1Yl9zaXplLCBtYXRjaCwgMCk7CiAgICBpZiAoc3RhdHVzICE9IDApCiAgICAgIHsKICAgICAgICByZWdlcnJvciAoc3RhdHVzLCAmcmUsIG1zZywgbXNnX3NpemUpOwogICAgICAgIGZwcmludGYgKHN0ZGVyciwgIiVzXG4iLCBtc2cpOwogICAgICB9CiAgICBlbHNlCiAgICAgIHsKICAgICAgICBmb3IgKG49MDsgbiA8IHN1Yl9zaXplOyBuKyspCiAgICAgICAgICB7CiAgICAgICAgICAgIGZvciAoZCA9IDAsIG0gPSBtYXRjaFtuXS5ybV9zbzsKICAgICAgICAgICAgICAgICBtID49IDAgJiYgbSA8IG1hdGNoW25dLnJtX2VvOwogICAgICAgICAgICAgICAgIG0rKywgZCsrKQogICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIHN1YltuXVtkXSA9IHN0cmluZ1ttXTsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgIHN1YltuXVtkXSA9ICdcMCc7CiAgICAgICAgICB9CiAgICAgIH0KICAgIHJlZ2ZyZWUgKCZyZSk7CiAgICByZXR1cm4gKHN0YXR1cyk7Cn0KCmludAptYWluICh2b2lkKQp7CiAgICBpbnQgICByZXN1bHQ7CiAgICBjaGFyICpzdHJpbmcgICAgICAgPSAiQ2lhbyBhbW9yZSBtaW8iOwogICAgY2hhciAqcmUgICAgICAgICAgID0gIipDaWFvIChhbW8pcmUiOwogICAgY2hhciAgc3ViMFsyMDBdOwogICAgICAgICAgc3ViMFswXSA9ICdcMCc7CiAgICBjaGFyICBzdWIxWzIwMF07CiAgICAgICAgICBzdWIxWzBdID0gJ1wwJzsKICAgIGNoYXIgKnN1YltdID0ge3N1YjAsIHN1YjF9OwogICAgCiAgICByZXN1bHQgPSByZWdleF9tYXRjaCAocmUsIHN0cmluZywgMiwgc3ViKTsKICAgIAogICAgaWYgKHJlc3VsdCA9PSAwKQogICAgICB7CiAgICAgICAgcHJpbnRmICgiSWwgbW9kZWxsbyBcIiVzXCIgdHJvdmEgY29ycmlzcG9uZGVuemEgIiwKICAgICAgICAgICAgICAgIHJlKTsKICAgICAgICBwcmludGYgKCJuZWxsYSBzdHJpbmdhIFwiJXNcIiwgcHJlY2lzYW1lbnRlICIsCiAgICAgICAgICAgICAgICBzdHJpbmcpOwogICAgICAgIHByaW50ZiAoIm5lbGxhIHBvcnppb25lIFwiJXNcIiwgbWVudHJlIGxhICIsCiAgICAgICAgICAgICAgICBzdWJbMF0pOwogICAgICAgIHByaW50ZiAoInNvdHRvc3RyaW5nYSBlc3RyYXR0YSDDqCBcIiVzXCIuXG4iLAogICAgICAgICAgICAgICAgc3ViWzFdKTsKICAgICAgfQogICAgZWxzZQogICAgICB7CiAgICAgICAgcHJpbnRmICgiSWwgbW9kZWxsbyBcIiVzXCIgIiwgcmUpOwogICAgICAgIHByaW50ZiAoIk5PTiB0cm92YSBjb3JyaXNwb25kZW56YSAiKTsKICAgICAgICBwcmludGYgKCJuZWxsYSBzdHJpbmdhIFwiJXNcIlxuIiwgc3RyaW5nKTsKICAgICAgfQogICAgcmV0dXJuIDA7Cn0=