我是 C 语言的新自行车,在了解了如何在 ANSI C 中使用正则表达式的一些简单示例和最佳实践之后。我有简单的测试,想要在下面的描述字符串中获取日期“{d(02/01/2019 )}-致电Ms.ha检查此案”。我的意思是想要得到“02/01/2019”这是我的代码
#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;
}
在这种情况下如何创建模式,
谢谢建议。