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