/[pcre]/code/tags/pcre-7.8/pcredemo.c
ViewVC logotype

Diff of /code/tags/pcre-7.8/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 315 by ph10, Fri Jan 25 17:23:16 2008 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  Compile thuswise:  In Unix-like environments, compile this program thuswise:
   gcc -Wall pcredemo.c -I/opt/local/include -L/opt/local/lib \  
     -R/opt/local/lib -lpcre  
11    
12  Replace "/opt/local/include" and "/opt/local/lib" with wherever the include and    gcc -Wall pcredemo.c -I/usr/local/include -L/usr/local/lib \
13  library files for PCRE are installed on your system. Only some operating      -R/usr/local/lib -lpcre
14    
15    Replace "/usr/local/include" and "/usr/local/lib" with wherever the include and
16    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 39  int subject_length; Line 49  int subject_length;
49  int rc, i;  int rc, i;
50    
51    
52  /*************************************************************************  /**************************************************************************
53  * 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  *
54  * the moment, "-g" to request repeated matching to find all occurrences, *  * the moment, "-g" to request repeated matching to find all occurrences,  *
55  * 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 *
56  * present. Apart from that, there must be exactly two arguments.         *  * if the -g option is present. Apart from that, there must be exactly two *
57  *************************************************************************/  * arguments.                                                              *
58    **************************************************************************/
59    
60  find_all = 0;  find_all = 0;
61  for (i = 1; i < argc; i++)  for (i = 1; i < argc; i++)
# Line 90  if (re == NULL) Line 101  if (re == NULL)
101    
102  /*************************************************************************  /*************************************************************************
103  * 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     *
104  * pattern match against the subject string. This just does ONE match. If *  * pattern match against the subject string. This does just ONE match. If *
105  * further matching is needed, it will be done below.                     *  * further matching is needed, it will be done below.                     *
106  *************************************************************************/  *************************************************************************/
107    
# Line 116  if (rc < 0) Line 127  if (rc < 0)
127      */      */
128      default: printf("Matching error %d\n", rc); break;      default: printf("Matching error %d\n", rc); break;
129      }      }
130      pcre_free(re);     /* Release memory used for the compiled pattern */
131    return 1;    return 1;
132    }    }
133    
# Line 126  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 149  for (i = 0; i < rc; i++) Line 161  for (i = 0; i < rc; i++)
161    }    }
162    
163    
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    
171  /* 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
172  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 231  if (namecount <= 0) printf("No named sub
231  * proceed round the loop.                                                *  * proceed round the loop.                                                *
232  *************************************************************************/  *************************************************************************/
233    
234  if (!find_all) return 0;   /* Finish unless -g was given */  if (!find_all)
235      {
236      pcre_free(re);   /* Release the memory used for the compiled pattern */
237      return 0;        /* Finish unless -g was given */
238      }
239    
240  /* Loop for second and subsequent matches */  /* Loop for second and subsequent matches */
241    
# Line 270  for (;;) Line 286  for (;;)
286    if (rc < 0)    if (rc < 0)
287      {      {
288      printf("Matching error %d\n", rc);      printf("Matching error %d\n", rc);
289        pcre_free(re);    /* Release memory used for the compiled pattern */
290      return 1;      return 1;
291      }      }
292    
# Line 310  for (;;) Line 327  for (;;)
327    }      /* End of loop to find second and subsequent matches */    }      /* End of loop to find second and subsequent matches */
328    
329  printf("\n");  printf("\n");
330    pcre_free(re);       /* Release memory used for the compiled pattern */
331  return 0;  return 0;
332  }  }
333    

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

  ViewVC Help
Powered by ViewVC 1.1.5