### Configure with custom compiler flags and installation prefix Source: https://www.pcre.org/current/doc/html/README.txt Example of running the configure script with specific C compiler flags and a custom installation directory. ```bash CFLAGS='-O2 -Wall' ./configure --prefix=/opt/local ``` -------------------------------- ### Quantified (*ACCEPT) example Source: https://www.pcre.org/current/doc/html/pcre2pattern.html Shows an example of (*ACCEPT) being used with quantification. ```regex (A(*ACCEPT)??B)C ``` -------------------------------- ### #newline_default example Source: https://www.pcre.org/current/doc/html/pcre2test.html Example of the #newline_default command with a list of acceptable newline types. ```plaintext #newline_default LF Any anyCRLF ``` -------------------------------- ### Callout pattern example Source: https://www.pcre.org/current/doc/html/pcre2callout.html Example pattern containing a callout. ```text ab(?C4)cd ``` -------------------------------- ### Pattern Start Options and Limits Source: https://www.pcre.org/current/doc/html/pcre2syntax.html Lists options and limits that are recognized only at the start of a pattern or after specific sequences. ```text (*LIMIT_DEPTH=d) set the backtracking limit to d (*LIMIT_HEAP=d) set the heap size limit to d * 1024 bytes (*LIMIT_MATCH=d) set the match limit to d (*CASELESS_RESTRICT) set PCRE2_EXTRA_CASELESS_RESTRICT when matching (*NOTEMPTY) set PCRE2_NOTEMPTY when matching (*NOTEMPTY_ATSTART) set PCRE2_NOTEMPTY_ATSTART when matching (*NO_AUTO_POSSESS) no auto-possessification (PCRE2_NO_AUTO_POSSESS) (*NO_DOTSTAR_ANCHOR) no .* anchoring (PCRE2_NO_DOTSTAR_ANCHOR) (*NO_JIT) disable JIT optimization (*NO_START_OPT) no start-match optimization (PCRE2_NO_START_OPTIMIZE) (*TURKISH_CASING) set PCRE2_EXTRA_TURKISH_CASING when matching (*UTF) set appropriate UTF mode for the library in use (*UCP) set PCRE2_UCP (use Unicode properties for \d etc) ``` -------------------------------- ### Branch reset group example Source: https://www.pcre.org/current/doc/html/pcre2pattern.html Example of a branch reset group (?| where alternatives use the same numbers for capturing parentheses. ```regex (?|(Sat)ur|(Sun))day ``` -------------------------------- ### Retrieve 8-bit library installation path Source: https://www.pcre.org/current/doc/html/README.txt Use the pcre2-config command to output information about the 8-bit library installation. ```bash pcre2-config --libs8 ``` -------------------------------- ### Quantifier Syntax Examples Source: https://www.pcre.org/current/doc/html/pcre2pattern.html Examples of various repetition quantifiers applied to literals, character classes, and digits. ```regex z{2,4} ``` ```regex [aeiou]{3,} ``` ```regex \d{8} ``` ```regex X{,4} is interpreted as X{0,4} ``` -------------------------------- ### Setting a locale example Source: https://www.pcre.org/current/doc/html/pcre2test.html Example of using the locale modifier to specify a locale name. ```text /pattern/locale=fr_FR ``` -------------------------------- ### Internal Option Scope Examples Source: https://www.pcre.org/current/doc/html/pcre2pattern.html Examples showing how internal option changes affect groups and subsequent pattern parts. ```text (a(?i)b)c ``` ```text (a(?i)b|c) ``` -------------------------------- ### Test substring extraction with copy and get modifiers Source: https://www.pcre.org/current/doc/html/pcre2test.html Example of using copy and get modifiers to test substring extraction functions with capture groups. ```text abcd\=copy=1,copy=3,get=G1 ``` -------------------------------- ### Get configure help Source: https://www.pcre.org/current/doc/html/pcre2build.html Run this command to see the complete list of options available for the configure script. ```bash ./configure --help ``` -------------------------------- ### Combined BSR configuration Source: https://www.pcre.org/current/doc/html/pcre2pattern.html An example of combining multiple configuration sequences at the start of a pattern. ```regex (*ANY)(*BSR_ANYCRLF) ``` -------------------------------- ### Configure static library build Source: https://www.pcre.org/current/doc/html/README.txt Example of configuring the build to produce fully statically linked binaries. ```bash LDFLAGS=--static ./configure --disable-shared ``` -------------------------------- ### Example of newline convention in pattern Source: https://www.pcre.org/current/doc/html/pcre2pattern.html A pattern starting with the (*CR) sequence to define the newline convention. ```text (*CR)a.b ``` -------------------------------- ### Stack pattern management example Source: https://www.pcre.org/current/doc/html/pcre2test.html Demonstrates pushing patterns onto a stack, saving/loading them, and popping them for matching. ```text /abc/push /xyz/push #save tempfile #load tempfile #pop info xyz #pop jit,bincode abc ``` -------------------------------- ### Pattern using \K to reset match start Source: https://www.pcre.org/current/doc/html/pcre2pattern.html Example of using \K to exclude 'foo' from the final matched sequence. ```regex foo\Kbar ``` -------------------------------- ### Callouts with string arguments Source: https://www.pcre.org/current/doc/html/pcre2test.html Example showing output format when using callouts with string arguments. ```text re> /^ab(?C'first')cd(?C"second")ef/ data> abcdefg Callout (7): 'first' --->abcdefg ^ ^ c Callout (20): "second" --->abcdefg ^ ^ e 0: abcdef ``` -------------------------------- ### Example pattern for DFA matching Source: https://www.pcre.org/current/doc/html/pcre2matching.html A sample pattern used to illustrate how the alternative matching algorithm handles multiple potential matches starting at the same point. ```regex cat(er(pillar)?)? ``` -------------------------------- ### Calling an external program from a pcre2grep callout Source: https://www.pcre.org/current/doc/html/pcre2grep.html When the callout string does not start with a pipe, it is parsed as an executable name and arguments separated by pipes. The example shows a call to /bin/echo with arguments and the resulting output. ```shell executable_name|arg1|arg2|... ``` ```shell echo -e "abcde\n12345" | pcre2grep \ '(?x)(.)(..(.) (?C"/bin/echo|Arg1: [$1] [$2] [$3]|Arg2: $|${1}$| ($4)")()' - Output: Arg1: [a] [bcd] [d] Arg2: |a| () abcde Arg1: [1] [234] [4] Arg2: |1| () 12345 ``` -------------------------------- ### Compile pcre2demo.c with standard library paths Source: https://www.pcre.org/current/doc/html/pcre2sample.html Compiles the demonstration program using the PCRE2 8-bit library when installed in standard include and library directories. ```bash cc -o pcre2demo pcre2demo.c -lpcre2-8 ``` -------------------------------- ### PCRE2_EXTENDED comment example Source: https://www.pcre.org/current/doc/html/pcre2pattern.html A pattern that demonstrates the PCRE2_EXTENDED comment syntax. An unescaped # starts a comment that continues to the next newline. The literal newline sequence in the pattern ends the comment; escape sequences that represent a newline do not count. ```regex abc #comment \n still comment ``` -------------------------------- ### PCRE2 demonstration program source code Source: https://www.pcre.org/current/doc/html/pcre2demo.html The full demonstration program source code, including necessary macros and main function setup. ```c /************************************************* * PCRE2 DEMONSTRATION PROGRAM * *************************************************/ /* This is a demonstration program to illustrate a straightforward way of using the PCRE2 regular expression library from a C program. See the pcre2sample documentation for a short discussion ("man pcre2sample" if you have the PCRE2 man pages installed). PCRE2 is a revised API for the library, and is incompatible with the original PCRE API. There are actually three libraries, each supporting a different code unit width. This demonstration program uses the 8-bit library. The default is to process each code unit as a separate character, but if the pattern begins with "(*UTF)", both it and the subject are treated as UTF-8 strings, where characters may occupy multiple code units. In Unix-like environments, if PCRE2 is installed in your standard system libraries, you should be able to compile this program using this command: cc -Wall pcre2demo.c -lpcre2-8 -o pcre2demo If PCRE2 is not installed in a standard place, it is likely to be installed with support for the pkg-config mechanism. If you have pkg-config, you can compile this program using this command: cc -Wall pcre2demo.c `pkg-config --cflags --libs libpcre2-8` -o pcre2demo If you do not have pkg-config, you may have to use something like this: cc -Wall pcre2demo.c -I/usr/local/include -L/usr/local/lib \ -R/usr/local/lib -lpcre2-8 -o pcre2demo Replace "/usr/local/include" and "/usr/local/lib" with wherever the include and library files for PCRE2 are installed on your system. Only some operating systems (Solaris is one) use the -R option. Building under Windows: If you want to statically link this program against a non-dll .a file, you must define PCRE2_STATIC before including pcre2.h, so in this environment, uncomment the following line. */ /* #define PCRE2_STATIC */ /* The PCRE2_CODE_UNIT_WIDTH macro must be defined before including pcre2.h. For a program that uses only one code unit width, setting it to 8, 16, or 32 makes it possible to use generic function names such as pcre2_compile(). Note that just changing 8 to 16 (for example) is not sufficient to convert this program to process 16-bit characters. Even in a fully 16-bit environment, where string-handling functions such as strcmp() and printf() work with 16-bit characters, the code for handling the table of named substrings will still need to be modified. */ #define PCRE2_CODE_UNIT_WIDTH 8 #include #include #include /************************************************************************** * Here is the program. The API includes the concept of "contexts" for * * setting up unusual interface requirements for compiling and matching, * * such as custom memory managers and non-standard newline definitions. * * This program does not do any of this, so it makes no use of contexts, * * always passing NULL where a context could be given. * **************************************************************************/ int main(int argc, char **argv) { pcre2_code *re; PCRE2_SPTR pattern; /* PCRE2_SPTR is a pointer to unsigned code units of */ PCRE2_SPTR subject; /* the appropriate width (in this case, 8 bits). */ PCRE2_SPTR name_table; int errornumber; int find_all, caseless_match; int i; int rc; uint32_t namecount; uint32_t name_entry_size; PCRE2_SIZE erroroffset; PCRE2_SIZE *ovector; PCRE2_SIZE ovector_last[2]; PCRE2_SIZE subject_length; pcre2_match_data *match_data; /************************************************************************** * First, sort out the command line. Options: * * - "-g" to request repeated matching to find all occurrences, * * like Perl's /g option. We set the variable find_all to a non-zero * * value if the -g option is present. * * - "-i" to request caseless matching, like Perl's /i option. We set the * * variable caseless_match to PCRE2_CASELESS if the -i option is * * present. * **************************************************************************/ find_all = 0; caseless_match = 0; for (i = 1; i < argc; i++) { if (strcmp(argv[i], "-g") == 0) find_all = 1; else if (strcmp(argv[i], "-i") == 0) caseless_match = PCRE2_CASELESS; else if (argv[i][0] == '-') { printf("Unrecognised option %s\n", argv[i]); return 1; } else break; } /* After the options, we require exactly two arguments, which are the pattern, and the subject string. */ ``` -------------------------------- ### Equivalent Unicode property specifications Source: https://www.pcre.org/current/doc/html/pcre2pattern.html Examples showing equivalent ways to specify the same Unicode property, demonstrating loose matching rules. ```regex \p{bidiclass=al} \p{BC=al} \p{ Bidi_Class : AL } \p{ Bi-di class = Al } \P{ ^ Bi-di class = Al } ``` -------------------------------- ### Compile pcre2demo.c with /usr/local paths Source: https://www.pcre.org/current/doc/html/pcre2sample.html Compiles the demonstration program when PCRE2 is installed under /usr/local, specifying the include and library directories. ```bash cc -o pcre2demo -I/usr/local/include pcre2demo.c -L/usr/local/lib -lpcre2-8 ``` -------------------------------- ### THEN verb alternation example Source: https://www.pcre.org/current/doc/html/pcre2pattern.html Example of using the THEN verb within an alternation. ```regex ( COND1 (*THEN) FOO | COND2 (*THEN) BAR | COND3 (*THEN) BAZ ) ... ``` -------------------------------- ### Single-threaded JIT Compilation and Execution Source: https://www.pcre.org/current/doc/html/pcre2jit.html This example demonstrates compiling a pattern with JIT, assigning a JIT stack, and executing a match without using a callback. ```c int rc; pcre2_code *re; pcre2_match_data *match_data; pcre2_match_context *mcontext; pcre2_jit_stack *jit_stack; re = pcre2_compile(pattern, PCRE2_ZERO_TERMINATED, 0, &errornumber, &erroffset, NULL); rc = pcre2_jit_compile(re, PCRE2_JIT_COMPLETE); mcontext = pcre2_match_context_create(NULL); jit_stack = pcre2_jit_stack_create(32*1024, 512*1024, NULL); pcre2_jit_stack_assign(mcontext, NULL, jit_stack); match_data = pcre2_match_data_create(re, 10); rc = pcre2_match(re, subject, length, 0, 0, match_data, mcontext); /* Process result */ pcre2_code_free(re); pcre2_match_data_free(match_data); pcre2_match_context_free(mcontext); pcre2_jit_stack_free(jit_stack); ``` -------------------------------- ### THEN verb nested alternation example Source: https://www.pcre.org/current/doc/html/pcre2pattern.html Example showing how THEN interacts with nested alternation. ```regex A (B(*THEN)C | (*FAIL)) | D ``` -------------------------------- ### SKIP verb usage example Source: https://www.pcre.org/current/doc/html/pcre2pattern.html Example of using the SKIP verb in a pattern. ```regex a+(*SKIP)b ``` -------------------------------- ### Match context setup actions: callout, limits, memory management Source: https://www.pcre.org/current/doc/html/pcre2_substitute.html The actions that can be performed using the match context, including setting a callout function, limiting matching offsets, modifying backtracking limits, and custom memory management. ```plaintext Set up a callout function Set a matching offset limit Change the backtracking match limit Change the backtracking depth limit Set custom memory management in the match context ``` -------------------------------- ### Substitution callout test Source: https://www.pcre.org/current/doc/html/pcre2test.html Example of setting up a substitution callout function to inspect input and output strings. ```text /abc/g,replace=<$0>,substitute_callout abcdefabcpqr 1(1) Old 0 3 "abc" New 0 5 "" 2(1) Old 6 9 "abc" New 8 13 "" 2: defpqr ``` -------------------------------- ### COMMIT verb usage example Source: https://www.pcre.org/current/doc/html/pcre2pattern.html Example of using the COMMIT verb in a pattern. ```regex a+(*COMMIT)b ``` -------------------------------- ### Run pcre2demo with sample commands Source: https://www.pcre.org/current/doc/html/pcre2sample.html Example invocations of the compiled pcre2demo program, including the -g option for global matching and -i for case-insensitive matching. ```bash ./pcre2demo 'cat|dog' 'the cat sat on the mat' ./pcre2demo -g 'cat|dog' 'the dog sat on the cat' ./pcre2demo -i 'cat' 'the dog sat on the CAT' ``` -------------------------------- ### Sequential backtracking verbs example Source: https://www.pcre.org/current/doc/html/pcre2pattern.html Example of using multiple backtracking verbs in sequence. ```regex ...(*COMMIT)(*PRUNE)... ``` -------------------------------- ### Multiple backtracking verbs example Source: https://www.pcre.org/current/doc/html/pcre2pattern.html Example of a pattern containing multiple backtracking verbs. ```regex (A(*COMMIT)B(*THEN)C|ABD) ``` -------------------------------- ### Example of (*ACCEPT) inside capturing parentheses Source: https://www.pcre.org/current/doc/html/pcre2pattern.html Demonstrates how (*ACCEPT) captures data when placed inside capturing parentheses. ```regex A((?:A|B(*ACCEPT)|C)D) ``` -------------------------------- ### Enable JIT compiler support Source: https://www.pcre.org/current/doc/html/pcre2build.html Options for enabling Just-in-time (JIT) compiler support during the build process. ```bash --enable-jit ``` ```bash --enable-jit=auto ``` -------------------------------- ### THEN verb conditional group example Source: https://www.pcre.org/current/doc/html/pcre2pattern.html Example of using the THEN verb within a conditional group. ```regex ^.*? (?(?=a) a | b(*THEN)c ) ``` -------------------------------- ### Anchored pattern example Source: https://www.pcre.org/current/doc/html/pcre2matching.html An example of an anchored pattern used to illustrate matching differences. ```text ^<.*> ``` -------------------------------- ### Displaying numerical callout arguments Source: https://www.pcre.org/current/doc/html/pcre2test.html Example of the default output for a numerical callout showing positions and the next pattern item. ```text --->pqrabcdef 0 ^ ^ \d ```