1 <html> 2 <head> 3 <title>pcre2demo specification</title> 4 </head> 5 <body bgcolor="#FFFFFF" text="#00005A" link="#0066FF" alink="#3399FF" vlink="#2222BB"> 6 <h1>pcre2demo man page</h1> 7 <p> 8 Return to the <a href="index.html">PCRE2 index page</a>. 9 </p> 10 <p> 11 This page is part of the PCRE2 HTML documentation. It was generated 12 automatically from the original man page. If there is any nonsense in it, 13 please consult the man page, in case the conversion went wrong. 14 <br> 15 <ul> 16 </ul> 17 <PRE> 18 /************************************************* 19 * PCRE2 DEMONSTRATION PROGRAM * 20 *************************************************/ 21 22 /* This is a demonstration program to illustrate a straightforward way of 23 using the PCRE2 regular expression library from a C program. See the 24 pcre2sample documentation for a short discussion ("man pcre2sample" if you have 25 the PCRE2 man pages installed). PCRE2 is a revised API for the library, and is 26 incompatible with the original PCRE API. 27 28 There are actually three libraries, each supporting a different code unit 29 width. This demonstration program uses the 8-bit library. The default is to 30 process each code unit as a separate character, but if the pattern begins with 31 "(*UTF)", both it and the subject are treated as UTF-8 strings, where 32 characters may occupy multiple code units. 33 34 In Unix-like environments, if PCRE2 is installed in your standard system 35 libraries, you should be able to compile this program using this command: 36 37 cc -Wall pcre2demo.c -lpcre2-8 -o pcre2demo 38 39 If PCRE2 is not installed in a standard place, it is likely to be installed 40 with support for the pkg-config mechanism. If you have pkg-config, you can 41 compile this program using this command: 42 43 cc -Wall pcre2demo.c `pkg-config --cflags --libs libpcre2-8` -o pcre2demo 44 45 If you do not have pkg-config, you may have to use something like this: 46 47 cc -Wall pcre2demo.c -I/usr/local/include -L/usr/local/lib \ 48 -R/usr/local/lib -lpcre2-8 -o pcre2demo 49 50 Replace "/usr/local/include" and "/usr/local/lib" with wherever the include and 51 library files for PCRE2 are installed on your system. Only some operating 52 systems (Solaris is one) use the -R option. 53 54 Building under Windows: 55 56 If you want to statically link this program against a non-dll .a file, you must 57 define PCRE2_STATIC before including pcre2.h, so in this environment, uncomment 58 the following line. */ 59 60 /* #define PCRE2_STATIC */ 61 62 /* The PCRE2_CODE_UNIT_WIDTH macro must be defined before including pcre2.h. 63 For a program that uses only one code unit width, setting it to 8, 16, or 32 64 makes it possible to use generic function names such as pcre2_compile(). Note 65 that just changing 8 to 16 (for example) is not sufficient to convert this 66 program to process 16-bit characters. Even in a fully 16-bit environment, where 67 string-handling functions such as strcmp() and printf() work with 16-bit 68 characters, the code for handling the table of named substrings will still need 69 to be modified. */ 70 71 #define PCRE2_CODE_UNIT_WIDTH 8 72 73 #include <stdio.h> 74 #include <string.h> 75 #include <pcre2.h> 76 77 78 /************************************************************************** 79 * Here is the program. The API includes the concept of "contexts" for * 80 * setting up unusual interface requirements for compiling and matching, * 81 * such as custom memory managers and non-standard newline definitions. * 82 * This program does not do any of this, so it makes no use of contexts, * 83 * always passing NULL where a context could be given. * 84 **************************************************************************/ 85 86 int main(int argc, char **argv) 87 { 88 pcre2_code *re; 89 PCRE2_SPTR pattern; /* PCRE2_SPTR is a pointer to unsigned code units of */ 90 PCRE2_SPTR subject; /* the appropriate width (in this case, 8 bits). */ 91 PCRE2_SPTR name_table; 92 93 int crlf_is_newline; 94 int errornumber; 95 int find_all; 96 int i; 97 int rc; 98 int utf8; 99 100 uint32_t option_bits; 101 uint32_t namecount; 102 uint32_t name_entry_size; 103 uint32_t newline; 104 105 PCRE2_SIZE erroroffset; 106 PCRE2_SIZE *ovector; 107 108 size_t subject_length; 109 pcre2_match_data *match_data; 110 111 112 113 /************************************************************************** 114 * First, sort out the command line. There is only one possible option at * 115 * the moment, "-g" to request repeated matching to find all occurrences, * 116 * like Perl's /g option. We set the variable find_all to a non-zero value * 117 * if the -g option is present. * 118 **************************************************************************/ 119 120 find_all = 0; 121 for (i = 1; i < argc; i++) 122 { 123 if (strcmp(argv[i], "-g") == 0) find_all = 1; 124 else if (argv[i][0] == '-') 125 { 126 printf("Unrecognised option %s\n", argv[i]); 127 return 1; 128 } 129 else break; 130 } 131 132 /* After the options, we require exactly two arguments, which are the pattern, 133 and the subject string. */ 134 135 if (argc - i != 2) 136 { 137 printf("Exactly two arguments required: a regex and a subject string\n"); 138 return 1; 139 } 140 141 /* As pattern and subject are char arguments, they can be straightforwardly 142 cast to PCRE2_SPTR as we are working in 8-bit code units. */ 143 144 pattern = (PCRE2_SPTR)argv[i]; 145 subject = (PCRE2_SPTR)argv[i+1]; 146 subject_length = strlen((char *)subject); 147 148 149 /************************************************************************* 150 * Now we are going to compile the regular expression pattern, and handle * 151 * any errors that are detected. * 152 *************************************************************************/ 153 154 re = pcre2_compile( 155 pattern, /* the pattern */ 156 PCRE2_ZERO_TERMINATED, /* indicates pattern is zero-terminated */ 157 0, /* default options */ 158 &errornumber, /* for error number */ 159 &erroroffset, /* for error offset */ 160 NULL); /* use default compile context */ 161 162 /* Compilation failed: print the error message and exit. */ 163 164 if (re == NULL) 165 { 166 PCRE2_UCHAR buffer[256]; 167 pcre2_get_error_message(errornumber, buffer, sizeof(buffer)); 168 printf("PCRE2 compilation failed at offset %d: %s\n", (int)erroroffset, 169 buffer); 170 return 1; 171 } 172 173 174 /************************************************************************* 175 * If the compilation succeeded, we call PCRE again, in order to do a * 176 * pattern match against the subject string. This does just ONE match. If * 177 * further matching is needed, it will be done below. Before running the * 178 * match we must set up a match_data block for holding the result. * 179 *************************************************************************/ 180 181 /* Using this function ensures that the block is exactly the right size for 182 the number of capturing parentheses in the pattern. */ 183 184 match_data = pcre2_match_data_create_from_pattern(re, NULL); 185 186 rc = pcre2_match( 187 re, /* the compiled pattern */ 188 subject, /* the subject string */ 189 subject_length, /* the length of the subject */ 190 0, /* start at offset 0 in the subject */ 191 0, /* default options */ 192 match_data, /* block for storing the result */ 193 NULL); /* use default match context */ 194 195 /* Matching failed: handle error cases */ 196 197 if (rc < 0) 198 { 199 switch(rc) 200 { 201 case PCRE2_ERROR_NOMATCH: printf("No match\n"); break; 202 /* 203 Handle other special cases if you like 204 */ 205 default: printf("Matching error %d\n", rc); break; 206 } 207 pcre2_match_data_free(match_data); /* Release memory used for the match */ 208 pcre2_code_free(re); /* data and the compiled pattern. */ 209 return 1; 210 } 211 212 /* Match succeded. Get a pointer to the output vector, where string offsets are 213 stored. */ 214 215 ovector = pcre2_get_ovector_pointer(match_data); 216 printf("Match succeeded at offset %d\n", (int)ovector[0]); 217 218 219 /************************************************************************* 220 * We have found the first match within the subject string. If the output * 221 * vector wasn't big enough, say so. Then output any substrings that were * 222 * captured. * 223 *************************************************************************/ 224 225 /* The output vector wasn't big enough. This should not happen, because we used 226 pcre2_match_data_create_from_pattern() above. */ 227 228 if (rc == 0) 229 printf("ovector was not big enough for all the captured substrings\n"); 230 231 /* We must guard against patterns such as /(?=.\K)/ that use \K in an assertion 232 to set the start of a match later than its end. In this demonstration program, 233 we just detect this case and give up. */ 234 235 if (ovector[0] > ovector[1]) 236 { 237 printf("\\K was used in an assertion to set the match start after its end.\n" 238 "From end to start the match was: %.*s\n", (int)(ovector[0] - ovector[1]), 239 (char *)(subject + ovector[1])); 240 printf("Run abandoned\n"); 241 pcre2_match_data_free(match_data); 242 pcre2_code_free(re); 243 return 1; 244 } 245 246 /* Show substrings stored in the output vector by number. Obviously, in a real 247 application you might want to do things other than print them. */ 248 249 for (i = 0; i < rc; i++) 250 { 251 PCRE2_SPTR substring_start = subject + ovector[2*i]; 252 size_t substring_length = ovector[2*i+1] - ovector[2*i]; 253 printf("%2d: %.*s\n", i, (int)substring_length, (char *)substring_start); 254 } 255 256 257 /************************************************************************** 258 * That concludes the basic part of this demonstration program. We have * 259 * compiled a pattern, and performed a single match. The code that follows * 260 * shows first how to access named substrings, and then how to code for * 261 * repeated matches on the same subject. * 262 **************************************************************************/ 263 264 /* See if there are any named substrings, and if so, show them by name. First 265 we have to extract the count of named parentheses from the pattern. */ 266 267 (void)pcre2_pattern_info( 268 re, /* the compiled pattern */ 269 PCRE2_INFO_NAMECOUNT, /* get the number of named substrings */ 270 &namecount); /* where to put the answer */ 271 272 if (namecount == 0) printf("No named substrings\n"); else 273 { 274 PCRE2_SPTR tabptr; 275 printf("Named substrings\n"); 276 277 /* Before we can access the substrings, we must extract the table for 278 translating names to numbers, and the size of each entry in the table. */ 279 280 (void)pcre2_pattern_info( 281 re, /* the compiled pattern */ 282 PCRE2_INFO_NAMETABLE, /* address of the table */ 283 &name_table); /* where to put the answer */ 284 285 (void)pcre2_pattern_info( 286 re, /* the compiled pattern */ 287 PCRE2_INFO_NAMEENTRYSIZE, /* size of each entry in the table */ 288 &name_entry_size); /* where to put the answer */ 289 290 /* Now we can scan the table and, for each entry, print the number, the name, 291 and the substring itself. In the 8-bit library the number is held in two 292 bytes, most significant first. */ 293 294 tabptr = name_table; 295 for (i = 0; i < namecount; i++) 296 { 297 int n = (tabptr[0] << 8) | tabptr[1]; 298 printf("(%d) %*s: %.*s\n", n, name_entry_size - 3, tabptr + 2, 299 (int)(ovector[2*n+1] - ovector[2*n]), subject + ovector[2*n]); 300 tabptr += name_entry_size; 301 } 302 } 303 304 305 /************************************************************************* 306 * If the "-g" option was given on the command line, we want to continue * 307 * to search for additional matches in the subject string, in a similar * 308 * way to the /g option in Perl. This turns out to be trickier than you * 309 * might think because of the possibility of matching an empty string. * 310 * What happens is as follows: * 311 * * 312 * If the previous match was NOT for an empty string, we can just start * 313 * the next match at the end of the previous one. * 314 * * 315 * If the previous match WAS for an empty string, we can't do that, as it * 316 * would lead to an infinite loop. Instead, a call of pcre2_match() is * 317 * made with the PCRE2_NOTEMPTY_ATSTART and PCRE2_ANCHORED flags set. The * 318 * first of these tells PCRE2 that an empty string at the start of the * 319 * subject is not a valid match; other possibilities must be tried. The * 320 * second flag restricts PCRE2 to one match attempt at the initial string * 321 * position. If this match succeeds, an alternative to the empty string * 322 * match has been found, and we can print it and proceed round the loop, * 323 * advancing by the length of whatever was found. If this match does not * 324 * succeed, we still stay in the loop, advancing by just one character. * 325 * In UTF-8 mode, which can be set by (*UTF) in the pattern, this may be * 326 * more than one byte. * 327 * * 328 * However, there is a complication concerned with newlines. When the * 329 * newline convention is such that CRLF is a valid newline, we must * 330 * advance by two characters rather than one. The newline convention can * 331 * be set in the regex by (*CR), etc.; if not, we must find the default. * 332 *************************************************************************/ 333 334 if (!find_all) /* Check for -g */ 335 { 336 pcre2_match_data_free(match_data); /* Release the memory that was used */ 337 pcre2_code_free(re); /* for the match data and the pattern. */ 338 return 0; /* Exit the program. */ 339 } 340 341 /* Before running the loop, check for UTF-8 and whether CRLF is a valid newline 342 sequence. First, find the options with which the regex was compiled and extract 343 the UTF state. */ 344 345 (void)pcre2_pattern_info(re, PCRE2_INFO_ALLOPTIONS, &option_bits); 346 utf8 = (option_bits & PCRE2_UTF) != 0; 347 348 /* Now find the newline convention and see whether CRLF is a valid newline 349 sequence. */ 350 351 (void)pcre2_pattern_info(re, PCRE2_INFO_NEWLINE, &newline); 352 crlf_is_newline = newline == PCRE2_NEWLINE_ANY || 353 newline == PCRE2_NEWLINE_CRLF || 354 newline == PCRE2_NEWLINE_ANYCRLF; 355 356 /* Loop for second and subsequent matches */ 357 358 for (;;) 359 { 360 uint32_t options = 0; /* Normally no options */ 361 PCRE2_SIZE start_offset = ovector[1]; /* Start at end of previous match */ 362 363 /* If the previous match was for an empty string, we are finished if we are 364 at the end of the subject. Otherwise, arrange to run another match at the 365 same point to see if a non-empty match can be found. */ 366 367 if (ovector[0] == ovector[1]) 368 { 369 if (ovector[0] == subject_length) break; 370 options = PCRE2_NOTEMPTY_ATSTART | PCRE2_ANCHORED; 371 } 372 373 /* If the previous match was not an empty string, there is one tricky case to 374 consider. If a pattern contains \K within a lookbehind assertion at the 375 start, the end of the matched string can be at the offset where the match 376 started. Without special action, this leads to a loop that keeps on matching 377 the same substring. We must detect this case and arrange to move the start on 378 by one character. The pcre2_get_startchar() function returns the starting 379 offset that was passed to pcre2_match(). */ 380 381 else 382 { 383 PCRE2_SIZE startchar = pcre2_get_startchar(match_data); 384 if (start_offset <= startchar) 385 { 386 if (startchar >= subject_length) break; /* Reached end of subject. */ 387 start_offset = startchar + 1; /* Advance by one character. */ 388 if (utf8) /* If UTF-8, it may be more */ 389 { /* than one code unit. */ 390 for (; start_offset < subject_length; start_offset++) 391 if ((subject[start_offset] & 0xc0) != 0x80) break; 392 } 393 } 394 } 395 396 /* Run the next matching operation */ 397 398 rc = pcre2_match( 399 re, /* the compiled pattern */ 400 subject, /* the subject string */ 401 subject_length, /* the length of the subject */ 402 start_offset, /* starting offset in the subject */ 403 options, /* options */ 404 match_data, /* block for storing the result */ 405 NULL); /* use default match context */ 406 407 /* This time, a result of NOMATCH isn't an error. If the value in "options" 408 is zero, it just means we have found all possible matches, so the loop ends. 409 Otherwise, it means we have failed to find a non-empty-string match at a 410 point where there was a previous empty-string match. In this case, we do what 411 Perl does: advance the matching position by one character, and continue. We 412 do this by setting the "end of previous match" offset, because that is picked 413 up at the top of the loop as the point at which to start again. 414 415 There are two complications: (a) When CRLF is a valid newline sequence, and 416 the current position is just before it, advance by an extra byte. (b) 417 Otherwise we must ensure that we skip an entire UTF character if we are in 418 UTF mode. */ 419 420 if (rc == PCRE2_ERROR_NOMATCH) 421 { 422 if (options == 0) break; /* All matches found */ 423 ovector[1] = start_offset + 1; /* Advance one code unit */ 424 if (crlf_is_newline && /* If CRLF is a newline & */ 425 start_offset < subject_length - 1 && /* we are at CRLF, */ 426 subject[start_offset] == '\r' && 427 subject[start_offset + 1] == '\n') 428 ovector[1] += 1; /* Advance by one more. */ 429 else if (utf8) /* Otherwise, ensure we */ 430 { /* advance a whole UTF-8 */ 431 while (ovector[1] < subject_length) /* character. */ 432 { 433 if ((subject[ovector[1]] & 0xc0) != 0x80) break; 434 ovector[1] += 1; 435 } 436 } 437 continue; /* Go round the loop again */ 438 } 439 440 /* Other matching errors are not recoverable. */ 441 442 if (rc < 0) 443 { 444 printf("Matching error %d\n", rc); 445 pcre2_match_data_free(match_data); 446 pcre2_code_free(re); 447 return 1; 448 } 449 450 /* Match succeded */ 451 452 printf("\nMatch succeeded again at offset %d\n", (int)ovector[0]); 453 454 /* The match succeeded, but the output vector wasn't big enough. This 455 should not happen. */ 456 457 if (rc == 0) 458 printf("ovector was not big enough for all the captured substrings\n"); 459 460 /* We must guard against patterns such as /(?=.\K)/ that use \K in an 461 assertion to set the start of a match later than its end. In this 462 demonstration program, we just detect this case and give up. */ 463 464 if (ovector[0] > ovector[1]) 465 { 466 printf("\\K was used in an assertion to set the match start after its end.\n" 467 "From end to start the match was: %.*s\n", (int)(ovector[0] - ovector[1]), 468 (char *)(subject + ovector[1])); 469 printf("Run abandoned\n"); 470 pcre2_match_data_free(match_data); 471 pcre2_code_free(re); 472 return 1; 473 } 474 475 /* As before, show substrings stored in the output vector by number, and then 476 also any named substrings. */ 477 478 for (i = 0; i < rc; i++) 479 { 480 PCRE2_SPTR substring_start = subject + ovector[2*i]; 481 size_t substring_length = ovector[2*i+1] - ovector[2*i]; 482 printf("%2d: %.*s\n", i, (int)substring_length, (char *)substring_start); 483 } 484 485 if (namecount == 0) printf("No named substrings\n"); else 486 { 487 PCRE2_SPTR tabptr = name_table; 488 printf("Named substrings\n"); 489 for (i = 0; i < namecount; i++) 490 { 491 int n = (tabptr[0] << 8) | tabptr[1]; 492 printf("(%d) %*s: %.*s\n", n, name_entry_size - 3, tabptr + 2, 493 (int)(ovector[2*n+1] - ovector[2*n]), subject + ovector[2*n]); 494 tabptr += name_entry_size; 495 } 496 } 497 } /* End of loop to find second and subsequent matches */ 498 499 printf("\n"); 500 pcre2_match_data_free(match_data); 501 pcre2_code_free(re); 502 return 0; 503 } 504 505 /* End of pcre2demo.c */ 506 <p> 507 Return to the <a href="index.html">PCRE2 index page</a>. 508 </p> 509