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);
}