Using regex(3)

Olivier Nicole on at cs.ait.ac.th
Wed Jun 22 04:11:30 GMT 2005


Hi,

I must missunderstand how to use regex(3).

>From what I read in the man page, pmatch[i].rm_so is the begining of
the i-th match in the regular expression and pmatch[i].rm-so is the
end.

So if I try to match the regex "a(.)c" on the string "abc" I should
have: pamtch[1].rm_so=1 and pmatch[1].rm_eo=2, that is matching the
substring "b".

I have run the short programm as follow:


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


main() {
  int ret;
  regex_t *preg;
  size_t nmatch;
  regmatch_t * pmatch;
  char * buffer="a(.)c";
  char * string="abc";

  preg=(regex_t*)malloc(sizeof(regex_t));
  if(preg==NULL) exit(-1);
  ret=regcomp(preg, buffer, REG_EXTENDED);
  printf("number of substrings=%d\n", preg->re_nsub);
  pmatch=(regmatch_t *)malloc(5000); /* make it big enough */
  if (pmatch==NULL) exit(-1);

  nmatch=0;
  ret=regexec(preg, string, nmatch, pmatch, 0);
  printf("return from regexec=%d\nnmatch=%d\np0.so=%d p0.eo=%d\np1.so=%d p1.eo=%d\np2.so=%d p2.eo=%d\np3.so=%d p3.eo=%d\n", ret, nmatch, pmatch[0].rm_so, pmatch[0].rm_eo, pmatch[1].rm_so, pmatch[1].rm_eo, pmatch[2].rm_so, pmatch[2].rm_eo, pmatch[3].rm_so, pmatch[3].rm_eo );
  nmatch=1;
  ret=regexec(preg, string, nmatch, pmatch, 0);
  printf("return from regexec=%d\nnmatch=%d\np0.so=%d p0.eo=%d\np1.so=%d p1.eo=%d\np2.so=%d p2.eo=%d\np3.so=%d p3.eo=%d\n", ret, nmatch, pmatch[0].rm_so, pmatch[0].rm_eo, pmatch[1].rm_so, pmatch[1].rm_eo, pmatch[2].rm_so, pmatch[2].rm_eo, pmatch[3].rm_so, pmatch[3].rm_eo );
  nmatch=2;
  ret=regexec(preg, string, nmatch, pmatch, 0);
  printf("return from regexec=%d\nnmatch=%d\np0.so=%d p0.eo=%d\np1.so=%d p1.eo=%d\np2.so=%d p2.eo=%d\np3.so=%d p3.eo=%d\n", ret, nmatch, pmatch[0].rm_so, pmatch[0].rm_eo, pmatch[1].rm_so, pmatch[1].rm_eo, pmatch[2].rm_so, pmatch[2].rm_eo, pmatch[3].rm_so, pmatch[3].rm_eo );

}

And the results I get are:

banyan<on>33: ./test
number of substrings=1
return from regexec=0
nmatch=0
p0.so=0 p0.eo=0
p1.so=0 p1.eo=0
p2.so=0 p2.eo=0
p3.so=0 p3.eo=0
return from regexec=0
nmatch=1
p0.so=0 p0.eo=0
p1.so=3 p1.eo=0
p2.so=0 p2.eo=0
p3.so=0 p3.eo=0
return from regexec=0
nmatch=2
p0.so=0 p0.eo=0
p1.so=3 p1.eo=0
p2.so=1 p2.eo=0
p3.so=2 p3.eo=0
banyan<on>34: 

Both on 4.10 releng and 5.3 releng, rm_eo is always empty and the
result is pushed in the next rm_so.

Any help appreciated.

Olivier


More information about the freebsd-questions mailing list