/[pcre]/code/trunk/pcredemo.c
ViewVC logotype

Diff of /code/trunk/pcredemo.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 63 by nigel, Sat Feb 24 21:40:03 2007 UTC revision 91 by nigel, Sat Feb 24 21:41:34 2007 UTC
# Line 7  of calling the PCRE regular expression l Line 7  of calling the PCRE regular expression l
7  pcresample documentation for a short discussion.  pcresample documentation for a short discussion.
8    
9  Compile thuswise:  Compile thuswise:
10    gcc -Wall pcredemo.c -I/opt/local/include -L/opt/local/lib \    gcc -Wall pcredemo.c -I/usr/local/include -L/usr/local/lib \
11      -R/opt/local/lib -lpcre      -R/usr/local/lib -lpcre
12    
13  Replace "/opt/local/include" and "/opt/local/lib" with wherever the include and  Replace "/usr/local/include" and "/usr/local/lib" with wherever the include and
14  library files for PCRE are installed on your system. Only some operating  library files for PCRE are installed on your system. Only some operating
15  systems (e.g. Solaris) use the -R option.  systems (e.g. Solaris) use the -R option.
16  */  */
# Line 39  int subject_length; Line 39  int subject_length;
39  int rc, i;  int rc, i;
40    
41    
42  /*************************************************************************  /**************************************************************************
43  * First, sort out the command line. There is only one possible option at *  * First, sort out the command line. There is only one possible option at  *
44  * the moment, "-g" to request repeated matching to find all occurrences, *  * the moment, "-g" to request repeated matching to find all occurrences,  *
45  * like Perl's /g option. We set the variable find_all non-zero if it is  *  * like Perl's /g option. We set the variable find_all to a non-zero value *
46  * present. Apart from that, there must be exactly two arguments.         *  * if the -g option is present. Apart from that, there must be exactly two *
47  *************************************************************************/  * arguments.                                                              *
48    **************************************************************************/
49    
50  find_all = 0;  find_all = 0;
51  for (i = 1; i < argc; i++)  for (i = 1; i < argc; i++)
# Line 90  if (re == NULL) Line 91  if (re == NULL)
91    
92  /*************************************************************************  /*************************************************************************
93  * If the compilation succeeded, we call PCRE again, in order to do a     *  * If the compilation succeeded, we call PCRE again, in order to do a     *
94  * pattern match against the subject string. This just does ONE match. If *  * pattern match against the subject string. This does just ONE match. If *
95  * further matching is needed, it will be done below.                     *  * further matching is needed, it will be done below.                     *
96  *************************************************************************/  *************************************************************************/
97    
# Line 116  if (rc < 0) Line 117  if (rc < 0)
117      */      */
118      default: printf("Matching error %d\n", rc); break;      default: printf("Matching error %d\n", rc); break;
119      }      }
120      pcre_free(re);     /* Release memory used for the compiled pattern */
121    return 1;    return 1;
122    }    }
123    
# Line 149  for (i = 0; i < rc; i++) Line 151  for (i = 0; i < rc; i++)
151    }    }
152    
153    
154  /*************************************************************************  /**************************************************************************
155  * That concludes the basic part of this demonstration program. We have   *  * That concludes the basic part of this demonstration program. We have    *
156  * compiled a pattern, and performed a single match. The code that follows*  * compiled a pattern, and performed a single match. The code that follows *
157  * first shows how to access named substrings, and then how to code for   *  * first shows how to access named substrings, and then how to code for    *
158  * repeated matches on the same subject.                                  *  * repeated matches on the same subject.                                   *
159  *************************************************************************/  **************************************************************************/
160    
161  /* See if there are any named substrings, and if so, show them by name. First  /* See if there are any named substrings, and if so, show them by name. First
162  we have to extract the count of named parentheses from the pattern. */  we have to extract the count of named parentheses from the pattern. */
# Line 219  if (namecount <= 0) printf("No named sub Line 221  if (namecount <= 0) printf("No named sub
221  * proceed round the loop.                                                *  * proceed round the loop.                                                *
222  *************************************************************************/  *************************************************************************/
223    
224  if (!find_all) return 0;   /* Finish unless -g was given */  if (!find_all)
225      {
226      pcre_free(re);   /* Release the memory used for the compiled pattern */
227      return 0;        /* Finish unless -g was given */
228      }
229    
230  /* Loop for second and subsequent matches */  /* Loop for second and subsequent matches */
231    
# Line 270  for (;;) Line 276  for (;;)
276    if (rc < 0)    if (rc < 0)
277      {      {
278      printf("Matching error %d\n", rc);      printf("Matching error %d\n", rc);
279        pcre_free(re);    /* Release memory used for the compiled pattern */
280      return 1;      return 1;
281      }      }
282    
# Line 310  for (;;) Line 317  for (;;)
317    }      /* End of loop to find second and subsequent matches */    }      /* End of loop to find second and subsequent matches */
318    
319  printf("\n");  printf("\n");
320    pcre_free(re);       /* Release memory used for the compiled pattern */
321  return 0;  return 0;
322  }  }
323    

Legend:
Removed from v.63  
changed lines
  Added in v.91

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12