Re: [gtk-list] regex.h...



On Thu, 19 Aug 1999, Alexander Sendzimir wrote:

Hi,

> I'm using regex.h to see what it can do. I can't get it to work properly
> (for what I THINK it should work for).
> 
> Matching ^([[:digit:]]+)[[:space:]]+(.*)$ against the string "4096
> four thousand ninety six" matches only the first group. In other words,
> the matches returned are
> 
>     "4096    four thousand ninety six"
>     "4096"
> 
> but no "four thousand ninety six". regcomp() sets re_nsub in the regex_t
> structure to 2 as it should be. When I place an extra set of parentheses
> around the last matching group it matches. That is,
> ^([[:digit:]]+)[[:space:]]+((.*)$) matches (notice ((.*)$) rather than
> (.*)$) the above string as
> 
>     "4096    four thousand ninety six"
>     "4096"
>     "four thousand ninety six"
> 
> Why can't I get by without the extra parens?

It is because, you doesn't read manual for regexec carrefully.
At ofsett 0 of regmatch_t array are indexes for part of string which
exactly match the whole pattern, at offset 1 is first substring and so on.
Look at attached program which I wrote, when I was learning how to use
regular patterns in C. It is very simple and show the whole secret of
POSIX RE patterns i C. It can by used for debuging RE patterns very well.
First parameter for this program is RE pattern and second is string which
should be matched.

I know that this post is bit offtopic, but I mean that this should be bit
usefull for more programmers on this list.

With regards,
Stevo.

---
Stefan Ondrejicka <ondrej@idata.sk>
Beethovenova 11, 917 08 Trnava, Slovakia
http://www.idata.sk/~ondrej/
#include <sys/types.h>
#include <string.h>
#include <regex.h>


int main(argc , argv)
int argc;
char **argv;
{
	regex_t preg;
	regmatch_t mtch[10];
	size_t rm,i;
	char pom[1024];

	rm = regcomp(&preg , argv[1] , REG_EXTENDED);

	(rm = regexec(&preg , argv[2] , 10 , mtch , 0)) ? printf("!match\n") : printf("match\n");

	for(i = 0; !rm && i<= preg.re_nsub; i++)
	{
		strncpy(pom , argv[2]+mtch[i].rm_so , mtch[i].rm_eo - mtch[i].rm_so);
		pom[mtch[i].rm_eo - mtch[i].rm_so] = '\0';
		printf("$%d = %s\n" , i , pom);
	}
	regfree(&preg);

	return 0;
}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]