#include <stdio.h>
#include <stdlib.h>
#include "xalloc.h"
#include "regex.h"
// compile:
// gcc -I coreutils-8.4/lib/ -c regex_test.cpp
// g++ -o regex_test regex_test.o coreutils-8.4/lib/xmalloc.o coreutils-8.4/lib/xalloc-die.o coreutils-8.4/lib/exitfail.o coreutils-8.4/lib/regex.o
void print_regerror (int errcode, regex_t *compiled)
{
size_t length = regerror (errcode, compiled, NULL, 0);
char *buffer = (char *)xmalloc (length);
if(!buffer) printf("error: regerror malloc failed!\n");
else {
(void) regerror (errcode, compiled, buffer, length);
printf("error: %s\n", buffer);
free(buffer);
}
}
int main(int argc, char *argv[]){
if(argc < 3) printf("usage: regex_test pattern string\n");
else {
regex_t rx;
int err;
if((err = regcomp(&rx, argv[1], REG_EXTENDED)))
print_regerror(err, &rx);
else {
regmatch_t matches[4];
if(!regexec(&rx, argv[2], 4, matches, 0)) {
int i;
printf("match! \n");
for(i = 0; i < 4; i++) {
if(matches[i].rm_so != -1)
printf(" s:%i, e:%i", matches[i].rm_so, matches[i].rm_eo);
}
printf("\n");
} else printf("match failed.\n");
regfree(&rx);
}
}
}
Here is the problem. If you execute:
Please advise. Thank you!