Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why regexec() in posix c always return the first match,how can it return all match positions only run once?

Tags:

c

regex

linux

Now when I want to return all match positions in str, such as:

abcd123abcd123abcd

Suppose I want to get all "abcd", I must use regexec(),get the first position:0, 3, then I will use:

123abcd123abcd

as the new string to use regexec() again, and so on. I read the manual about regexec(), it says:

int regexec(const regex_t *preg, const char *string, size_t nmatch,
               regmatch_t pmatch[], int eflags);
nmatch and pmatch are used to provide information regarding the location of any 
matches.

but why doesn't this work? This is my code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <regex.h>

int main(int argc, char **argv)
{
   int i = 0;
   int res;
   int len;
   char result[BUFSIZ];
   char err_buf[BUFSIZ];
   char* src = argv[1];  

   const char* pattern = "\\<[^,;]+\\>";
   regex_t preg;

   regmatch_t pmatch[10];

   if( (res = regcomp(&preg, pattern, REG_EXTENDED)) != 0)
   {
      regerror(res, &preg, err_buf, BUFSIZ);
      printf("regcomp: %s\n", err_buf);
      exit(res);
   }

   res = regexec(&preg, src, 10, pmatch, REG_NOTBOL);
   //~ res = regexec(&preg, src, 10, pmatch, 0);
   //~ res = regexec(&preg, src, 10, pmatch, REG_NOTEOL);
   if(res == REG_NOMATCH)
   {
      printf("NO match\n");
      exit(0);
   }
   for (i = 0; pmatch[i].rm_so != -1; i++)
   {
      len = pmatch[i].rm_eo - pmatch[i].rm_so;
      memcpy(result, src + pmatch[i].rm_so, len);
      result[len] = 0;
      printf("num %d: '%s'\n", i, result);
   }
   regfree(&preg);
   return 0;
}

./regex 'hello, world'

the output:

num 0: 'hello'

this is my respect outputs:

num 0: 'hello'
num 1: 'world'
like image 939
gnemoug Avatar asked May 07 '13 11:05

gnemoug


People also ask

What is the use of regexec in POSIX?

POSIX regex matching. regexec () is used to match a null-terminated string against the precompiled pattern buffer, preg. nmatch and pmatch are used to provide information regarding the location of any matches. eflags may be the bitwise- or of one or both of REG_NOTBOL and REG_NOTEOL which cause changes in matching behavior described below.

How do I use regcomp in POSIX?

POSIX regex compiling regcomp () is used to compile a regular expression into a form that is suitable for subsequent regexec () searches. regcomp () is supplied with preg, a pointer to a pattern buffer storage area; regex, a pointer to the null-terminated string and cflags, flags used to determine the type of compilation.

What is the difference between regexec () and Reg_nomatch ()?

This regexec () function returns 0 if there is successful matching done and REG_NOMATCH if the string does not match.

What is regcomp () function in regex?

regex is a pointer to a memory location where expression is matched and stored. Return Value: This returns the value as shown below: 0: when successful compilation is done. Error_code: When there is unsuccessful complilation of the expression. Below is the illustration of the regcomp () function:


1 Answers

regexec performs a regex match. Once a match has been found regexec will return zero (i.e. successful match). The parameter pmatch will contain information about that one match. The first array index (i.e. zero) will contain the entire match, subsequent array indices contain information about capture groups/sub-expressions.

To demonstrate:

const char* pattern = "(\\w+) (\\w+)";

matched on "hello world" will output:

num 0: 'hello world'  - entire match
num 1: 'hello'        - capture group 1
num 2: 'world'        - capture group 2

(see it in action)

In most regex environments the behaviour you seek could have been gotten by using the global modifier: /g. Regexec does not provide this modifier as a flag nor does it support modifiers. You will therefore have to loop while regexec returns zero starting from the last character of the previous match to get all matches.

The global modifier is also not available using the PCRE library (famous regex C library). The PCRE man pages have this to say about it:

By calling pcre_exec() multiple times with appropriate arguments, you can mimic Perl's /g option

like image 60
Lodewijk Bogaards Avatar answered Oct 12 '22 21:10

Lodewijk Bogaards