home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / unix / question / 13176 < prev    next >
Encoding:
Text File  |  1992-11-09  |  1.7 KB  |  61 lines

  1. Newsgroups: comp.unix.questions
  2. Path: sparky!uunet!ukma!cs.widener.edu!eff!news.oc.com!mercury.unt.edu!usenet
  3. From: <cpearce@nemesis.acs.unt.edu> (Chris Pearce)
  4. Subject: Problems with regular expressions...
  5. Message-ID: <1992Nov9.231745.13734@mercury.unt.edu>
  6. Sender: usenet@mercury.unt.edu (UNT USENet Adminstrator)
  7. Organization: University of North Texas
  8. Date: Mon, 9 Nov 1992 23:17:45 GMT
  9. Lines: 50
  10.  
  11. I stumped comp.sys.hp with this one. Maybe someone here knows what is
  12. happening.
  13.  
  14. HP9000/425; HPUX 8.0.
  15.  
  16. I'm using the routines regcomp() and regexec(). I believe that are part of
  17. POSIX 2. I'm trying to either match line in a file containing a keyword
  18. or a keyword and its argument. I'm trying to skip whitespace. I'm trying
  19. to match only full lines that match. I'm getting no matches.
  20.  
  21. Here's a code snippet...
  22.  
  23. regex_t r1, r2;
  24.  
  25. init ()
  26. {
  27.     r1 = regcomp (&r1, "^ *#keyword *$", REG_EXTENDED);
  28.     r2 = regcomp (&r2, "^ *#keyword +([^ ]+) *$", REG_EXTENDED);
  29. }
  30.  
  31. match ()
  32. {
  33.     char str [256];
  34.     regmatch_t rm [2];
  35.  
  36. /* Open file, read lines into _str_ until EOF */
  37. ..
  38.     if (!regexec (&r1, str, r1.re_nsub, rm, NULL))
  39.         printf ("Matched first expression.\n
  40.     else if (!regexec (&r2, str, r2.re_nsub, rm, NULL))
  41.         printf ("Matched second expression.\n");
  42.     else
  43.         printf ("No match.\n");
  44. }
  45.  
  46. main ()
  47. {
  48.     init();
  49.     match ();
  50. }
  51.  
  52. I hope all that code above is typo-free: I can't go back to edit it....
  53. When the code runs, I get no matches. Either I'm missing something
  54. glaringly obvious (I hope not), I'm getting bit by some strange subtlety,
  55. or HPs routines are goofed. I've read the man pages (regexec, regcomp,
  56. and regexp(5)) carefully, but I still can't figure out the problem.
  57.  
  58. Any ideas?
  59. ---
  60. Chris Pearce, cpearce@nemesis.acs.unt.edu
  61.