regex(3)

Från Wiki.linux.se
Version från den 20 november 2024 kl. 17.22 av Admin (diskussion | bidrag)
(skillnad) ← Äldre version | Nuvarande version (skillnad) | Nyare version → (skillnad)
Hoppa till navigering Hoppa till sök
  1. include <stdint.h>
  2. include <stdio.h>
  3. include <stdlib.h>
  4. include <regex.h>
  1. define ARRAY_SIZE(arr) (sizeof((arr)) / sizeof((arr)[0]))

static const char *const str =

       "1) John Driverhacker;\n2) John Doe;\n3) John Foo;\n";

static const char *const re = "John.*o";

int main(void) {

   static const char *s = str;
   regex_t     regex;
   regmatch_t  pmatch[1];
   regoff_t    off, len;
   if (regcomp(&regex, re, REG_NEWLINE))
       exit(EXIT_FAILURE);
   printf("String = \"%s\"\n", str);
   printf("Matches:\n");
   for (unsigned int i = 0; ; i++) {
       if (regexec(&regex, s, ARRAY_SIZE(pmatch), pmatch, 0))
           break;
       off = pmatch[0].rm_so + (s - str);
       len = pmatch[0].rm_eo - pmatch[0].rm_so;
       printf("#%zu:\n", i);
       printf("offset = %jd; length = %jd\n", (intmax_t) off,
              (intmax_t) len);
       printf("substring = \"%.*s\"\n", len, s + pmatch[0].rm_so);
       s += pmatch[0].rm_eo;
   }
   exit(EXIT_SUCCESS);

}