[ create a new paste ] login | about

Link: http://codepad.org/onqxRRyX    [ raw code | output | fork ]

appunti2 - C, pasted on May 1:
#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);
        fprintf (stderr, "%s\n", msg);
        return (status);
      }
    status = regexec (&re, string, sub_size, match, 0);
    if (status != 0)
      {
        regerror (status, &re, msg, msg_size);
        fprintf (stderr, "%s\n", msg);
      }
    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;
}


Output:
1
2
Invalid preceding regular expression
Il modello "*Ciao (amo)re" NON trova corrispondenza nella stringa "Ciao amore mio"


Create a new paste based on this one


Comments: