home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / sys / hp / 12740 < prev    next >
Encoding:
Text File  |  1992-11-10  |  1.9 KB  |  70 lines

  1. Path: sparky!uunet!ogicse!uwm.edu!caen!sdd.hp.com!hp-col!fc.hp.com!ec
  2. From: ec@fc.hp.com (Edgar_Circenis)
  3. Newsgroups: comp.sys.hp
  4. Subject: Re: Still problems with regcomp() and regexec()
  5. Message-ID: <BxICsC.I3F@fc.hp.com>
  6. Date: 10 Nov 92 16:07:23 GMT
  7. Article-I.D.: fc.BxICsC.I3F
  8. References: <1992Nov9.183202.17452@mercury.unt.edu>
  9. Sender: news@fc.hp.com (news daemon)
  10. Organization: Hewlett-Packard Fort Collins Site
  11. Lines: 56
  12. X-Newsreader: TIN [version 1.1.4 PL6]
  13.  
  14.  
  15.  (cpearce@nemesis.acs.unt.edu) wrote:
  16. > Sorry, I made a mistake on the transcription of my code... though
  17. > my initial example has
  18. >     if (regexec (...))
  19. >         printf ("Matched.\n");
  20. > my actual code has
  21. >     if (!regexec (...))
  22. >         printf ("Matched.\n");
  23.  
  24. OK, I actually looked at the code this time.  I do not understand why
  25. your code doesn't match anything.  The code I have works fine for the
  26. expressions you provided.  I assume you are trying to match strings
  27. like "#keyword" and "#keyword token".
  28.  
  29. One problem I found in your code had to do with how you were processing
  30. the subexpressions.  Regexec always places pointers to the matching text
  31. for the whole pattern in rm[0].  rm[1] is the first subexpression match,
  32. rm[2] is the second, and so on.  When you reference r1.re_nsub, it is zero
  33. and r2.re_nsub is one.  So, in both cases, you were passing a value that
  34. was too small.  Also, in the example you provided, you were printing
  35. out rm[1].rm_sp and rm[2].rm_ep.  Try this:
  36.  
  37. #include <regex.h>
  38.  
  39. regex_t r1, r2;
  40.  
  41. void init()
  42. {
  43.     regcomp (&r1, "^ *#keyword *$", REG_EXTENDED);
  44.     regcomp (&r2, "^ *#keyword +([^ ]+) *$", REG_EXTENDED);
  45. }
  46.  
  47. void match()
  48. {
  49.     char        str[255];
  50.     regmatch_t    rm[2];
  51.  
  52.     while(gets(str)) {
  53.         if (!regexec (&r1, str, r1.re_nsub+1, rm, NULL))
  54.             printf ("Matched.\n");
  55.         if (!regexec (&r2, str, r2.re_nsub+1, rm, NULL))
  56.             printf ("Matched. sp = %x; ep = %x\n", rm[1].rm_sp, rm[1].rm_ep);
  57.     }
  58. }
  59.  
  60. main ()
  61. {
  62.     init ();
  63.     match ();
  64. }
  65.  
  66. Edgar
  67.