#regex example help

7 messages · Page 1 of 1 (latest)

finite pumiceBOT
#

When your question is answered use !solved or the button below to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

leaden oxide
#
#include <regex.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define ARRAY_SIZE(arr) (sizeof((arr)) / sizeof((arr)[0]))
#define MATCH_CAP (16)

char **match_regex(const char *regex_string, const char *input_string,
                   size_t *num_matches) {

  regex_t regex;
  regmatch_t pmatch[1];
  regoff_t off, len;

  const char *s = input_string;
  size_t matches_cap = MATCH_CAP;

  char **matches = malloc(sizeof(char *) * MATCH_CAP);

  if (regcomp(&regex, regex_string, REG_NEWLINE | REG_EXTENDED))
    return NULL; // will update error handling later

  for (size_t matches_size = 0;; matches_size++) {

    if (regexec(&regex, s, ARRAY_SIZE(pmatch), pmatch, 0)) {
      *num_matches = matches_size; // set the number of matches before returning
      break;
    }

    off = pmatch[0].rm_so +
        (int)(s - input_string); // off represents start of match + the relative dist of start and input
    len = pmatch[0].rm_eo - pmatch[0].rm_so;

    if (matches_size == matches_cap) {

      char **ptr = realloc(matches, sizeof(char *) * matches_cap * 2);
      matches_cap *= 2;

      if (ptr == NULL) {
        perror("realloc()");
        return NULL;
      }
      matches = ptr; // update matches ptr
    }

    matches[matches_size] = strndup(input_string + off, (size_t)len);

    s += pmatch[0].rm_eo; // move string ptr past this match
  }

  return matches;
}

int main(void) {
  const char *const input_string =
      "1) John Driverhacker;\n2) John Doe;\n3) John Foo;\n";
  const char *const regex_string = "John.*o";
  size_t num_matches = 0;

  char **matches = match_regex(regex_string, input_string, &num_matches);

  if (!matches) {

    puts("Found no matches"
         "exiting...");
    exit(EXIT_SUCCESS);
  }

  puts("Found matches:");
  for (size_t i = 0; i < num_matches; i++) {
    printf("- %s\n", matches[i]);
    free(matches[i]);
  }
  free(matches);

  exit(EXIT_SUCCESS);
}
leaden oxide
#

Here are my flags: CFLAGS = -std=$(VER) -g -Wall -Werror -Wconversion -fanalyzer -fsanitize=undefined,leak -fsanitize-trap=undefined

#

It runs fine without fsanitize=leak, but I want to know if I'm doing something wrong or if this is just something to ignore this time.

leaden oxide
#

Here is an MRE: ```c
#include <regex.h>
#include <stdlib.h>

int match_regex(char *str, char *re) {

regex_t regex;

regcomp(&regex, re, REG_NEWLINE);

regexec(&regex, str, 0, NULL, 0);
}

int main(void) {

char *str = "1) John Driverhacker;\n2) John Doe;\n3) John Foo;\n";
char *re = "John.*o";

match_regex(str, re);

exit(EXIT_SUCCESS);
}

finite pumiceBOT
# finite pumice

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity