home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!ogicse!uwm.edu!caen!sdd.hp.com!hp-col!fc.hp.com!ec
- From: ec@fc.hp.com (Edgar_Circenis)
- Newsgroups: comp.sys.hp
- Subject: Re: Still problems with regcomp() and regexec()
- Message-ID: <BxICsC.I3F@fc.hp.com>
- Date: 10 Nov 92 16:07:23 GMT
- Article-I.D.: fc.BxICsC.I3F
- References: <1992Nov9.183202.17452@mercury.unt.edu>
- Sender: news@fc.hp.com (news daemon)
- Organization: Hewlett-Packard Fort Collins Site
- Lines: 56
- X-Newsreader: TIN [version 1.1.4 PL6]
-
-
- (cpearce@nemesis.acs.unt.edu) wrote:
- > Sorry, I made a mistake on the transcription of my code... though
- > my initial example has
- >
- > if (regexec (...))
- > printf ("Matched.\n");
- >
- > my actual code has
- >
- > if (!regexec (...))
- > printf ("Matched.\n");
-
- OK, I actually looked at the code this time. I do not understand why
- your code doesn't match anything. The code I have works fine for the
- expressions you provided. I assume you are trying to match strings
- like "#keyword" and "#keyword token".
-
- One problem I found in your code had to do with how you were processing
- the subexpressions. Regexec always places pointers to the matching text
- for the whole pattern in rm[0]. rm[1] is the first subexpression match,
- rm[2] is the second, and so on. When you reference r1.re_nsub, it is zero
- and r2.re_nsub is one. So, in both cases, you were passing a value that
- was too small. Also, in the example you provided, you were printing
- out rm[1].rm_sp and rm[2].rm_ep. Try this:
-
- #include <regex.h>
-
- regex_t r1, r2;
-
- void init()
- {
- regcomp (&r1, "^ *#keyword *$", REG_EXTENDED);
- regcomp (&r2, "^ *#keyword +([^ ]+) *$", REG_EXTENDED);
- }
-
- void match()
- {
- char str[255];
- regmatch_t rm[2];
-
- while(gets(str)) {
- if (!regexec (&r1, str, r1.re_nsub+1, rm, NULL))
- printf ("Matched.\n");
- if (!regexec (&r2, str, r2.re_nsub+1, rm, NULL))
- printf ("Matched. sp = %x; ep = %x\n", rm[1].rm_sp, rm[1].rm_ep);
- }
- }
-
- main ()
- {
- init ();
- match ();
- }
-
- Edgar
-