| 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 |
*/ |
*/ |
| 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++) |
| 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 |
|
|
| 117 |
*/ |
*/ |
| 118 |
default: printf("Matching error %d\n", rc); break; |
default: printf("Matching error %d\n", rc); break; |
| 119 |
} |
} |
| 120 |
|
free(re); /* Release memory used for the compiled pattern */ |
| 121 |
return 1; |
return 1; |
| 122 |
} |
} |
| 123 |
|
|
| 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. */ |
| 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 |
|
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 |
|
|
| 276 |
if (rc < 0) |
if (rc < 0) |
| 277 |
{ |
{ |
| 278 |
printf("Matching error %d\n", rc); |
printf("Matching error %d\n", rc); |
| 279 |
|
free(re); /* Release memory used for the compiled pattern */ |
| 280 |
return 1; |
return 1; |
| 281 |
} |
} |
| 282 |
|
|
| 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 |
|
free(re); /* Release memory used for the compiled pattern */ |
| 321 |
return 0; |
return 0; |
| 322 |
} |
} |
| 323 |
|
|