I need to get the groups that matches with my Regex in C to manipulate a Java program logs.
I have tested the Regex:
(Client:\s[a-zA-Z\s]+)|(Wallet:\s[a-zA-Z0-9]+)|(ID\s*:\s*[0-9]{3}.{0,1}[0-9]{3}.{0,1}[0-9]{3}-{0,1}[0-9]{2})
here and it works.
But in my C program, it doesn't work as well.
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
const char *source =
"[com.example.app.JavaClass.JavaMethod(JavaClass.java:1)] (Thread-1) - "
"Client: FirstName MiddleName AnotherName LastName, Wallet: WL01, "
"Agency: 9999, ID: 06611486123, Ticket: TKR211";
const char *regexString =
"(Client:\\s[a-zA-Z[:space:]]+)|(Wallet:\\s[a-zA-Z0-9]+)|(ID\\s*:\\s*[0-"
"9]{3}.{0,1}[0-9]{3}.{0,1}[0-9]{3}-{0,1}[0-9]{2})";
regex_t regexCompiled;
regcomp(®exCompiled, regexString, REG_ICASE | REG_EXTENDED);
size_t ngroups = regexCompiled.re_nsub + 1;
regmatch_t *groups = malloc(ngroups * sizeof(regmatch_t));
regexec(®exCompiled, source, ngroups, groups, 0);
char cursorCopy[strlen(source) + 1];
strcpy(cursorCopy, source);
size_t nmatched;
for (nmatched = 0; nmatched < ngroups; nmatched++) {
if (groups[nmatched].rm_so == (size_t)(-1)) {
break;
}
char *match =
calloc(groups[nmatched].rm_eo - groups[nmatched].rm_so, sizeof(char));
memcpy(match, &source[groups[nmatched].rm_so],
groups[nmatched].rm_eo - groups[nmatched].rm_so);
printf("Match: [%2u-%2u]: \"%s\"\n", groups[nmatched].rm_so,
groups[nmatched].rm_eo, match);
}
regfree(®exCompiled);
return 0;
}
Executing:
$ gcc -Wall -Wextra -Wwrite-strings reg.c && ./a.out
Generates the output:
Match: [70-119]: "Client: FirstName MiddleName AnotherName LastName"
Match: [70-119]: "Client: FirstName MiddleName AnotherName LastName"
But what I want is:
Match: [xx-xx]: "Client: FirstName MiddleName AnotherName LastName"
Match: [xx-xx]: "Wallet: WL01"
Match: [xx-xx]: "ID: 06611486123"
Can someone tell me if it is possible to do using C or I need another approach?
edit:
In my case is possible that some fields ("Client", "Wallet" or "ID") won't coming in log.


[:space:]in the C source. Read up on group 0..{0,1}?)[possible?]C is considered a universal programming language.)