0

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;```

2 Answers2

0

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.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • thanks so much but is possible return inside of array ?? – RubensBarrichello77 Dec 20 '21 at 11:07
  • @RubensBarrichello77 What kind of array do you have in mind? Please provide exact type. See [Proper way to copy C strings](https://stackoverflow.com/questions/9593798/proper-way-to-copy-c-strings) and [How do I create an array of strings in C?](https://stackoverflow.com/questions/1088622/how-do-i-create-an-array-of-strings-in-c/1095006) also. – Wiktor Stribiżew Dec 20 '21 at 11:12
  • example i want analize a text , and in this text can retrun 3 word or 50 word i dont know how many word return and how many long is the word , how is possible insert in array all in dynamic mode i suppose – RubensBarrichello77 Dec 20 '21 at 11:42
  • @RubensBarrichello77 You are not making it any clearer. Please edit the question with a sample array you have in mind. You should always provide exected output in the question. – Wiktor Stribiżew Dec 20 '21 at 11:44
  • sorry , i dont know what array return sure is char array , but i dont know dimention, the dimention depend of how many word find in text , – RubensBarrichello77 Dec 21 '21 at 07:57
  • @RubensBarrichello77 So you want some kind of `char arr[x][y]`? – Wiktor Stribiżew Dec 21 '21 at 07:59
  • sorry i try to explain much better , i want like final result somthing like this Local $aArray = StringRegExp('a b c', '(?i)(.*?)', 3) custom function , that match somthing (i specify example like this '(?i)(.*?)' ) in global mode (i specify like this 3) for all text or phrases i give it , i want sotred my result or many result (is variable) in array , i think i must use realloc? and incrase the array ? – RubensBarrichello77 Dec 21 '21 at 15:44
0

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);
}
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 20 '21 at 12:15