Re: regex.h...
- From: Alexander Sendzimir <sendzimir earthlink net>
- To: gtk-list redhat com
- Subject: Re: regex.h...
- Date: Fri, 20 Aug 1999 11:11:19 -0400
Stevo,
Actually, I did read the regex manpage and other sources of information quite
carefully. The problem is that re_nsub is not consistent with the number of
actual matches (including the match to the entire pattern). My code now works
just fine thanks to your example. You wrote:
regex( &preg, argv[2], 10, mtch, 0 )
^^
The number 10 was the give away that I was 1 off in my number of matches. I
just wasn't putting 2 and 2 together, so to speak. I had pulled the original
method of using regex.h from "Linux Application Development" by Johnson and
Troan. They don't point this out in their book. Although I haven't tried
explicitly, I suspect the code they provide won't work properly.
Thank you for your help.
abs
Stefan Ondrejicka wrote:
> Hi,
>
> 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/
>
> ------------------------------------------------------------------------
> Name: re_test.c
> re_test.c Type: Plain Text (TEXT/PLAIN)
> Encoding: BASE64
--
Alexander Sendzimir
Digitally Inclined
sendzimir@earthlink.net | 203 263 7405 | Woodbury, CT 06798-3017
#include <stdio.h>
#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]