hi is possible return in C (with library #include <regex.h> ) a group of my finded word ?, i find only example match and return number of position , how is possible modify , for return the exact word in this position ? thanks
regmatch_t pm;
char *s="mikko mikko";
regoff_t last_match=0;
regcomp(&p, "k", 0);
while(regexec(&p, s last_match, 1, &pm, 0) == 0) {
printf("start=%d end=%d\n", pm.rm_so last_match, pm.rm_eo last_match);
last_match = pm.rm_so 1;```
CodePudding user response:
You can use
#include <sys/types.h>
#include <regex.h>
#include <stdio.h>
#include <string.h>
void match_all(regex_t *p, char *sz) {
regmatch_t whole_match;
int match = 0;
size_t offset = 0;
size_t length = strlen(sz);
char result[BUFSIZ];
int len;
while (regexec(p, sz offset, 1, &whole_match, 0) == 0) {
match = 1;
len = whole_match.rm_eo - whole_match.rm_so;
memcpy(result, sz whole_match.rm_so, len);
result[len] = 0;
printf("Match: %s\n", result);
offset = whole_match.rm_eo 1; // increase the starting offset
if (offset > length) {
break;
}
}
if (! match) {
printf("\"%s\" does not contain a match\n", sz);
}
}
int main(int argc, char* argv[]) {
int r;
regex_t p;
r = regcomp(&p, "[[:alnum:]]*k[[:alnum:]]*", 0);
if (r != 0) {
printf("regcomp failed\n");
}
match_all(&p, "mikko mikko");
regfree(&p);
return 0;
}
See the online demo. Output:
Match: mikko
Match: mikko
The [[:alnum:]]*k[[:alnum:]]*
regex matches zero or more alphanumeric chars, k
and then again zero or more alphanumeric.
CodePudding user response:
this is my code
#include "string.h"
#include "stdlib.h"
#include <stdio.h>
#include <regex.h>
typedef struct {
int *array;
size_t used;
size_t size;
} Array;
void initArray(Array *a, size_t initialSize) {
a->array = malloc(initialSize * sizeof(int));
a->used = 0;
a->size = initialSize;
}
void insertArray(Array *a, int element) {
if (a->used == a->size) {
a->size *= 2;
a->array = realloc(a->array, a->size * sizeof(int));
}
a->array[a->used ] = element;
}
void freeArray(Array *a) {
free(a->array);
a->array = NULL;
a->used = a->size = 0;
}
char result[BUFSIZ];
Array MatchResult;
int CtrlMatcH;
void match_all(regex_t *p, char *sz) {
regmatch_t whole_match;
int match = 0;
size_t offset = 0;
size_t length = strlen(sz);
int len;
int ctrl=0;
initArray(&MatchResult, 5); // initially 5 elements
// sz= 1;
// return how many match have
while (regexec(p, sz offset, 1, &whole_match, 0) == 0) {
match = 1;
len = whole_match.rm_eo - whole_match.rm_so;
memcpy(result, sz whole_match.rm_so, len);
result[len] = 0;
printf("Match: %s\n", result);
size_t sz = strlen(result) 1
insertArray(&MatchResult, sz);
ctrl;
if (ctrl == 1) {
strcpy(MatchResult, result);
}
strcat(MatchResult, result);
offset = whole_match.rm_eo 1; // increase the starting offset
if (offset > length) {
break;
}
}
printf("%s\n", MatchResult);
if (! match) {
printf("\"%s\" does not contain a match\n", sz);
}
}
RING_FUNC(ring_stringregexp)
{
int argc;
char* argv;
int r;
regex_t p;
r = regcomp(&p, "[[:alnum:]]*k[[:alnum:]]*", 0);
if (r != 0) {
printf("regcomp failed\n");
}
match_all(&p, "mikko mikko");
printf("Match2: %s\n", result);
regfree(&p);
}
RING_API void ringlib_init(RingState *pRingState)
{
ring_vm_funcregister("stringregexp",ring_stringregexp);
}