I’m a new bike in C language, after some simple examples and best practices of how to use regular expressions in ANSI C.
I have simple test, want to get date in an description string bellow
“{d(02/01/2019)}-call Ms.ha check this case”. I mean want to get “02/01/2019”
this is my code
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <regex.h>
#define ARRAY_SIZE(arr) (sizeof((arr)) / sizeof((arr)[0]))
// initialize target string and regular expression string
int main(int argc, char **argv)
{
regex_t preg;
char *string = "{d(02/01/2019)}-call Ms.ha check this case";
char *pattern="?[0-9]+(\/[0-9]+)+(\/[0-9]+)?"; // I'm stupid pattern in here.
size_t nmatch = 2;
regmatch_t pmatch[2];
regcomp(&preg, pattern, REG_EXTENDED);
regexec(&preg, string, nmatch, pmatch, 0);
printf("a matched substring "%.*s" is found at position %d to %d.n",
pmatch[1].rm_eo - pmatch[1].rm_so, &string[pmatch[1].rm_so],
pmatch[1].rm_so, pmatch[1].rm_eo - 1);
regfree(&preg);
return EXIT_SUCCESS;
}
How to create pattern in this case,
Thanks Advice.
New contributor
VietV is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.