/[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 91 by nigel, Sat Feb 24 21:41:34 2007 UTC revision 442 by ph10, Fri Sep 11 10:21:02 2009 UTC
# Line 4  Line 4 
4    
5  /* This is a demonstration program to illustrate the most straightforward ways  /* This is a demonstration program to illustrate the most straightforward ways
6  of calling the PCRE regular expression library from a C program. See the  of calling the PCRE regular expression library from a C program. See the
7  pcresample documentation for a short discussion.  pcresample documentation for a short discussion ("man pcresample" if you have
8    the PCRE man pages installed).
9    
10    In Unix-like environments, compile this program thuswise:
11    
 Compile thuswise:  
12    gcc -Wall pcredemo.c -I/usr/local/include -L/usr/local/lib \    gcc -Wall pcredemo.c -I/usr/local/include -L/usr/local/lib \
13      -R/usr/local/lib -lpcre      -R/usr/local/lib -lpcre
14    
15  Replace "/usr/local/include" and "/usr/local/lib" with wherever the include and  Replace "/usr/local/include" and "/usr/local/lib" with wherever the include and
16  library files for PCRE are installed on your system. Only some operating  library files for PCRE are installed on your system. You don't need -I and -L
17    if PCRE is installed in the standard system libraries. Only some operating
18  systems (e.g. Solaris) use the -R option.  systems (e.g. Solaris) use the -R option.
 */  
19    
20    Building under Windows:
21    
22    If you want to statically link this program against a non-dll .a file, you must
23    define PCRE_STATIC before including pcre.h, otherwise the pcre_malloc() and
24    pcre_free() exported functions will be declared __declspec(dllimport), with
25    unwanted results. So in this environment, uncomment the following line. */
26    
27    /* #define PCRE_STATIC */
28    
29  #include <stdio.h>  #include <stdio.h>
30  #include <string.h>  #include <string.h>
# Line 128  printf("\nMatch succeeded at offset %d\n Line 138  printf("\nMatch succeeded at offset %d\n
138    
139  /*************************************************************************  /*************************************************************************
140  * We have found the first match within the subject string. If the output *  * We have found the first match within the subject string. If the output *
141  * vector wasn't big enough, set its size to the maximum. Then output any *  * vector wasn't big enough, say so. Then output any substrings that were *
142  * substrings that were captured.                                         *  * captured.                                                              *
143  *************************************************************************/  *************************************************************************/
144    
145  /* The output vector wasn't big enough */  /* The output vector wasn't big enough */
# Line 154  for (i = 0; i < rc; i++) Line 164  for (i = 0; i < rc; i++)
164  /**************************************************************************  /**************************************************************************
165  * That concludes the basic part of this demonstration program. We have    *  * That concludes the basic part of this demonstration program. We have    *
166  * compiled a pattern, and performed a single match. The code that follows *  * compiled a pattern, and performed a single match. The code that follows *
167  * first shows how to access named substrings, and then how to code for    *  * shows first how to access named substrings, and then how to code for    *
168  * repeated matches on the same subject.                                   *  * repeated matches on the same subject.                                   *
169  **************************************************************************/  **************************************************************************/
170    
# Line 213  if (namecount <= 0) printf("No named sub Line 223  if (namecount <= 0) printf("No named sub
223  *                                                                        *  *                                                                        *
224  * If the previous match WAS for an empty string, we can't do that, as it *  * If the previous match WAS for an empty string, we can't do that, as it *
225  * would lead to an infinite loop. Instead, a special call of pcre_exec() *  * would lead to an infinite loop. Instead, a special call of pcre_exec() *
226  * is made with the PCRE_NOTEMPTY and PCRE_ANCHORED flags set. The first  *  * is made with the PCRE_NOTEMPTY_ATSTART and PCRE_ANCHORED flags set.    *
227  * of these tells PCRE that an empty string is not a valid match; other   *  * The first of these tells PCRE that an empty string at the start of the *
228  * possibilities must be tried. The second flag restricts PCRE to one     *  * subject is not a valid match; other possibilities must be tried. The   *
229  * match attempt at the initial string position. If this match succeeds,  *  * second flag restricts PCRE to one match attempt at the initial string  *
230  * an alternative to the empty string match has been found, and we can    *  * position. If this match succeeds, an alternative to the empty string   *
231  * proceed round the loop.                                                *  * match has been found, and we can proceed round the loop.               *
232  *************************************************************************/  *************************************************************************/
233    
234  if (!find_all)  if (!find_all)
# Line 241  for (;;) Line 251  for (;;)
251    if (ovector[0] == ovector[1])    if (ovector[0] == ovector[1])
252      {      {
253      if (ovector[0] == subject_length) break;      if (ovector[0] == subject_length) break;
254      options = PCRE_NOTEMPTY | PCRE_ANCHORED;      options = PCRE_NOTEMPTY_ATSTART | PCRE_ANCHORED;
255      }      }
256    
257    /* Run the next matching operation */    /* Run the next matching operation */

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

  ViewVC Help
Powered by ViewVC 1.1.5