/[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 97 by ph10, Mon Mar 5 12:36:47 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  */  */
17    
18    
19    #ifdef HAVE_CONFIG_H
20    #  include <config.h>
21    #endif
22    
23  #include <stdio.h>  #include <stdio.h>
24  #include <string.h>  #include <string.h>
25  #include <pcre.h>  #include <pcre.h>
# Line 39  int subject_length; Line 43  int subject_length;
43  int rc, i;  int rc, i;
44    
45    
46  /*************************************************************************  /**************************************************************************
47  * 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  *
48  * the moment, "-g" to request repeated matching to find all occurrences, *  * the moment, "-g" to request repeated matching to find all occurrences,  *
49  * 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 *
50  * present. Apart from that, there must be exactly two arguments.         *  * if the -g option is present. Apart from that, there must be exactly two *
51  *************************************************************************/  * arguments.                                                              *
52    **************************************************************************/
53    
54  find_all = 0;  find_all = 0;
55  for (i = 1; i < argc; i++)  for (i = 1; i < argc; i++)
# Line 90  if (re == NULL) Line 95  if (re == NULL)
95    
96  /*************************************************************************  /*************************************************************************
97  * 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     *
98  * pattern match against the subject string. This just does ONE match. If *  * pattern match against the subject string. This does just ONE match. If *
99  * further matching is needed, it will be done below.                     *  * further matching is needed, it will be done below.                     *
100  *************************************************************************/  *************************************************************************/
101    
# Line 116  if (rc < 0) Line 121  if (rc < 0)
121      */      */
122      default: printf("Matching error %d\n", rc); break;      default: printf("Matching error %d\n", rc); break;
123      }      }
124      pcre_free(re);     /* Release memory used for the compiled pattern */
125    return 1;    return 1;
126    }    }
127    
# Line 149  for (i = 0; i < rc; i++) Line 155  for (i = 0; i < rc; i++)
155    }    }
156    
157    
158  /*************************************************************************  /**************************************************************************
159  * That concludes the basic part of this demonstration program. We have   *  * That concludes the basic part of this demonstration program. We have    *
160  * compiled a pattern, and performed a single match. The code that follows*  * compiled a pattern, and performed a single match. The code that follows *
161  * 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    *
162  * repeated matches on the same subject.                                  *  * repeated matches on the same subject.                                   *
163  *************************************************************************/  **************************************************************************/
164    
165  /* 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
166  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 225  if (namecount <= 0) printf("No named sub
225  * proceed round the loop.                                                *  * proceed round the loop.                                                *
226  *************************************************************************/  *************************************************************************/
227    
228  if (!find_all) return 0;   /* Finish unless -g was given */  if (!find_all)
229      {
230      pcre_free(re);   /* Release the memory used for the compiled pattern */
231      return 0;        /* Finish unless -g was given */
232      }
233    
234  /* Loop for second and subsequent matches */  /* Loop for second and subsequent matches */
235    
# Line 270  for (;;) Line 280  for (;;)
280    if (rc < 0)    if (rc < 0)
281      {      {
282      printf("Matching error %d\n", rc);      printf("Matching error %d\n", rc);
283        pcre_free(re);    /* Release memory used for the compiled pattern */
284      return 1;      return 1;
285      }      }
286    
# Line 310  for (;;) Line 321  for (;;)
321    }      /* End of loop to find second and subsequent matches */    }      /* End of loop to find second and subsequent matches */
322    
323  printf("\n");  printf("\n");
324    pcre_free(re);       /* Release memory used for the compiled pattern */
325  return 0;  return 0;
326  }  }
327    

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

  ViewVC Help
Powered by ViewVC 1.1.5