/[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 200 by ph10, Wed Aug 1 09:10:40 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. You don't need -I and -L
15    if PCRE is installed in the standard system libraries. Only some operating
16  systems (e.g. Solaris) use the -R option.  systems (e.g. Solaris) use the -R option.
17  */  */
18    
# Line 39  int subject_length; Line 40  int subject_length;
40  int rc, i;  int rc, i;
41    
42    
43  /*************************************************************************  /**************************************************************************
44  * 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  *
45  * the moment, "-g" to request repeated matching to find all occurrences, *  * the moment, "-g" to request repeated matching to find all occurrences,  *
46  * 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 *
47  * present. Apart from that, there must be exactly two arguments.         *  * if the -g option is present. Apart from that, there must be exactly two *
48  *************************************************************************/  * arguments.                                                              *
49    **************************************************************************/
50    
51  find_all = 0;  find_all = 0;
52  for (i = 1; i < argc; i++)  for (i = 1; i < argc; i++)
# Line 90  if (re == NULL) Line 92  if (re == NULL)
92    
93  /*************************************************************************  /*************************************************************************
94  * 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     *
95  * pattern match against the subject string. This just does ONE match. If *  * pattern match against the subject string. This does just ONE match. If *
96  * further matching is needed, it will be done below.                     *  * further matching is needed, it will be done below.                     *
97  *************************************************************************/  *************************************************************************/
98    
# Line 116  if (rc < 0) Line 118  if (rc < 0)
118      */      */
119      default: printf("Matching error %d\n", rc); break;      default: printf("Matching error %d\n", rc); break;
120      }      }
121      pcre_free(re);     /* Release memory used for the compiled pattern */
122    return 1;    return 1;
123    }    }
124    
# Line 149  for (i = 0; i < rc; i++) Line 152  for (i = 0; i < rc; i++)
152    }    }
153    
154    
155  /*************************************************************************  /**************************************************************************
156  * That concludes the basic part of this demonstration program. We have   *  * That concludes the basic part of this demonstration program. We have    *
157  * compiled a pattern, and performed a single match. The code that follows*  * compiled a pattern, and performed a single match. The code that follows *
158  * 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    *
159  * repeated matches on the same subject.                                  *  * repeated matches on the same subject.                                   *
160  *************************************************************************/  **************************************************************************/
161    
162  /* 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
163  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 222  if (namecount <= 0) printf("No named sub
222  * proceed round the loop.                                                *  * proceed round the loop.                                                *
223  *************************************************************************/  *************************************************************************/
224    
225  if (!find_all) return 0;   /* Finish unless -g was given */  if (!find_all)
226      {
227      pcre_free(re);   /* Release the memory used for the compiled pattern */
228      return 0;        /* Finish unless -g was given */
229      }
230    
231  /* Loop for second and subsequent matches */  /* Loop for second and subsequent matches */
232    
# Line 270  for (;;) Line 277  for (;;)
277    if (rc < 0)    if (rc < 0)
278      {      {
279      printf("Matching error %d\n", rc);      printf("Matching error %d\n", rc);
280        pcre_free(re);    /* Release memory used for the compiled pattern */
281      return 1;      return 1;
282      }      }
283    
# Line 310  for (;;) Line 318  for (;;)
318    }      /* End of loop to find second and subsequent matches */    }      /* End of loop to find second and subsequent matches */
319    
320  printf("\n");  printf("\n");
321    pcre_free(re);       /* Release memory used for the compiled pattern */
322  return 0;  return 0;
323  }  }
324    

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

  ViewVC Help
Powered by ViewVC 1.1.5