### Example Call to pcre2_dfa_match() Source: https://github.com/pcre2project/pcre2/blob/main/doc/pcre2.txt Demonstrates a basic invocation of pcre2_dfa_match() with necessary setup for workspace and match data. ```c int wspace[20]; pcre2_match_data *md = pcre2_match_data_create(4, NULL); int rc = pcre2_dfa_match( re, /* result of pcre2_compile() */ "some string", /* the subject string */ 11, /* the length of the subject string */ 0, /* start at offset 0 in the subject */ 0, /* default options */ md, /* the match data block */ NULL, /* a match context; NULL means use defaults */ wspace, /* working space vector */ 20); /* number of elements (NOT size in bytes) */ ``` -------------------------------- ### Installation of Documentation and Headers Source: https://github.com/pcre2project/pcre2/blob/main/CMakeLists.txt Installs header files, man pages, and HTML/text documentation to their respective installation directories. ```cmake install(FILES ${PCRE2_HEADERS} ${PCRE2POSIX_HEADERS} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) file(GLOB html ${PROJECT_SOURCE_DIR}/doc/html/*.html ${PROJECT_SOURCE_DIR}/doc/html/*.txt) file( GLOB txts ${PROJECT_SOURCE_DIR}/doc/*.txt AUTHORS.md COPYING ChangeLog LICENCE.md NEWS README SECURITY.md ) file(GLOB man1 ${PROJECT_SOURCE_DIR}/doc/*.1) file(GLOB man3 ${PROJECT_SOURCE_DIR}/doc/*.3) install(FILES ${man1} DESTINATION ${CMAKE_INSTALL_MANDIR}/man1) install(FILES ${man3} DESTINATION ${CMAKE_INSTALL_MANDIR}/man3) install(FILES ${txts} DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/doc/pcre2) install(FILES ${html} DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/doc/pcre2/html) ``` -------------------------------- ### PCRE2 JIT Compilation and Matching Example Source: https://github.com/pcre2project/pcre2/blob/main/doc/html/pcre2jit.html Demonstrates the setup and usage of PCRE2 with JIT compilation and matching. This includes compiling a pattern, enabling JIT, setting up match context and JIT stack, performing a match, and cleaning up resources. ```c 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); ``` -------------------------------- ### pcre2test: DFA Matching Example Source: https://github.com/pcre2project/pcre2/blob/main/doc/html/pcre2test.html Illustrates the output when using the alternative DFA matching function, showing all matches starting at the first possible point. ```bash re> /(tang|tangerine|tan)/ data> yellow tangerine\=dfa 0: tangerine 1: tang 2: tan ``` -------------------------------- ### Installation of pkg-config and Configuration Files Source: https://github.com/pcre2project/pcre2/blob/main/CMakeLists.txt Installs pkg-config files and the main pcre2-config script. It also configures and installs the pcre2-config.cmake and pcre2-config-version.cmake files. ```cmake install(FILES ${pkg_config_files} DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig) install( FILES "${PROJECT_BINARY_DIR}/pcre2-config" DESTINATION ${CMAKE_INSTALL_BINDIR} # Set 0755 permissions PERMISSIONS OWNER_WRITE OWNER_READ OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE ) # CMake config files. set(PCRE2_CONFIG_IN ${PROJECT_SOURCE_DIR}/cmake/pcre2-config.cmake.in) set(PCRE2_CONFIG_OUT ${PROJECT_BINARY_DIR}/cmake/pcre2-config.cmake) configure_package_config_file(${PCRE2_CONFIG_IN} ${PCRE2_CONFIG_OUT} INSTALL_DESTINATION ${PCRE2_INSTALL_CMAKEDIR}) set(PCRE2_CONFIG_VERSION_OUT ${PROJECT_BINARY_DIR}/cmake/pcre2-config-version.cmake) write_basic_package_version_file( ${PCRE2_CONFIG_VERSION_OUT} VERSION ${PCRE2_MAJOR}.${PCRE2_MINOR}.0 COMPATIBILITY SameMajorVersion ) install(FILES ${PCRE2_CONFIG_OUT} ${PCRE2_CONFIG_VERSION_OUT} DESTINATION ${PCRE2_INSTALL_CMAKEDIR}) ``` -------------------------------- ### Compile PCRE2 Demo Program (Standard Install) Source: https://github.com/pcre2project/pcre2/blob/main/doc/html/pcre2sample.html Command to compile the pcre2demo program when PCRE2 is installed in standard system directories. ```bash cc -o pcre2demo pcre2demo.c -lpcre2-8 ``` -------------------------------- ### Compile PCRE2 Demo Program (Custom Install) Source: https://github.com/pcre2project/pcre2/blob/main/doc/html/pcre2sample.html Command to compile the pcre2demo program when PCRE2 is installed in a non-standard location, specifying include and library paths. ```bash cc -o pcre2demo -I/usr/local/include pcre2demo.c -L/usr/local/lib -lpcre2-8 ``` -------------------------------- ### PCRE2 COMMIT Verb Example Source: https://github.com/pcre2project/pcre2/blob/main/doc/html/pcre2pattern.html The \*COMMIT verb forces the entire match to fail if backtracking reaches it. It commits to the current starting point, preventing further attempts to advance the starting position. ```regex a+(\*COMMIT)b ``` -------------------------------- ### Get Ovector Pointer Source: https://github.com/pcre2project/pcre2/blob/main/doc/html/pcre2api.html Retrieves a pointer to the ovector, which contains the start and end offsets of captured substrings. ```APIDOC ## pcre2_get_ovector_pointer ### Description Returns a pointer to the ovector, which is an array of PCRE2_SIZE values storing the offsets of captured substrings. The ovector is part of the match data block. ### Signature ```c PCRE2_SIZE *pcre2_get_ovector_pointer(pcre2_match_data *match_data); ``` ### Parameters * `match_data` (*pcre2_match_data* *) - A pointer to the match data block containing the ovector. ``` -------------------------------- ### Example: Single-threaded JIT Stack Usage Source: https://github.com/pcre2project/pcre2/blob/main/doc/pcre2.txt This example demonstrates setting up and using a JIT stack without a callback. Error checking is omitted for brevity but should be included in real applications. ```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); ``` -------------------------------- ### Get PCRE2 Start Character Offset Source: https://github.com/pcre2project/pcre2/blob/main/doc/html/pcre2api.html Retrieves the offset within the subject string where the current match began. ```c PCRE2_SIZE pcre2_get_startchar(pcre2_match_data *_match_data_); ``` -------------------------------- ### CMake Project Setup Source: https://github.com/pcre2project/pcre2/blob/main/maint/cmake-tests/build-interface/CMakeLists.txt Initializes a CMake project, sets the C standard, and ensures it's required. This is standard practice for C projects using CMake. ```cmake cmake_minimum_required(VERSION 3.15) project(TestBuildInterface C) set(CMAKE_C_STANDARD 99) set(CMAKE_C_STANDARD_REQUIRED TRUE) ``` -------------------------------- ### PCRE2 (*SKIP) Verb Example Source: https://github.com/pcre2project/pcre2/blob/main/doc/pcre2.txt Illustrates the (*SKIP) verb's behavior in advancing the starting point for subsequent match attempts. When the subject is 'aaaac...', after the first match attempt fails, the starting point skips to 'c' instead of the next character. ```regex a+(*SKIP)b ``` -------------------------------- ### Option with data examples Source: https://github.com/pcre2project/pcre2/blob/main/doc/pcre2grep.txt Demonstrates different ways to specify data for options like -f and --file. For short options, data can follow immediately or in the next argument. For long options, data can be separated by '=' or be in the next argument. ```bash -f/some/file ``` ```bash -f /some/file ``` ```bash --file=/some/file ``` ```bash --file /some/file ``` -------------------------------- ### Get PCRE2 Ovector Pointer Source: https://github.com/pcre2project/pcre2/blob/main/doc/html/pcre2api.html Returns a pointer to the beginning of the vector that stores the offsets of captured substrings. The offsets are pairs of start and end positions. ```c PCRE2_SIZE *pcre2_get_ovector_pointer(pcre2_match_data *_match_data_); ``` -------------------------------- ### Interactive pcre2test Run Example Source: https://github.com/pcre2project/pcre2/blob/main/doc/pcre2test.txt Demonstrates an interactive session with pcre2test, showing pattern matching and output for successful matches and 'No match' scenarios. ```bash $ pcre2test PCRE2 version 10.22 2016-07-29 re> /^abc(\d+)/ data> abc123 0: abc123 1: 123 data> xyz No match ``` -------------------------------- ### Automatic .* Anchoring Example (Enabled) Source: https://github.com/pcre2project/pcre2/blob/main/doc/pcre2.txt Demonstrates automatic anchoring of '.*' when it's the first significant item and PCRE2_DOTALL is set. All match attempts start at the beginning of the subject. ```regex .*\d ``` -------------------------------- ### Build and Compile PCRE2 with JIT Support Source: https://github.com/pcre2project/pcre2/blob/main/README.md This snippet shows how to clone the PCRE2 repository, initialize submodules for JIT support, build the library using CMake with JIT enabled, and then compile a demo C program that links against the built PCRE2 library. ```bash PCRE2_VERSION=10.42 # Fetch PCRE2 with 'git clone', or use curl/wget to download a release. # Here, let's use git to check out a release tag: git clone https://github.com/PCRE2Project/pcre2.git ./pcre2 \ --branch pcre2-$PCRE2_VERSION \ -c advice.detachedHead=false --depth 1 # If using the JIT, remember to fetch the Git submodule: (cd ./pcre2; git submodule update --init) # Now let's build PCRE2: (cd ./pcre2; \ cmake -G Ninja -DCMAKE_BUILD_TYPE=Debug \ -DPCRE2_SUPPORT_JIT=ON -B build; \ cmake --build build/) # Great, PCRE2 is built. # Here's a quick little demo to show how we can make use of PCRE2. # For a fuller example, see './pcre2/src/pcre2demo.c'. # See below for the demo code. # Compile the demo: gcc -g -I./pcre2/build -L./pcre2/build demo.c -o demo -lpcre2-8 # Finally, run our demo: ./demo 'c.t' 'dogs and cats' ``` -------------------------------- ### Run PCRE2 Demo Program (Basic Match) Source: https://github.com/pcre2project/pcre2/blob/main/doc/html/pcre2sample.html Example of running the pcre2demo program to match a simple pattern against a subject string. ```bash ./pcre2demo 'cat|dog' 'the cat sat on the mat' ``` -------------------------------- ### Substring Assertion Example Source: https://github.com/pcre2project/pcre2/blob/main/doc/html/pcre2pattern.html Demonstrates using a substring assertion to find a word containing 'rh' not at the start. This pattern uses a captured group and then scans its content. ```regex \\b(\\w++)(*scs:1.+rh) ``` -------------------------------- ### Compile PCRE2 Demo Program (Custom Install) Source: https://github.com/pcre2project/pcre2/blob/main/doc/pcre2.txt Compile the PCRE2 demonstration program when PCRE2 is installed in a custom location, such as /usr/local. This command specifies include and library paths. ```bash cc -o pcre2demo -I/usr/local/include pcre2demo.c \ -L/usr/local/lib -lpcre2-8 ``` -------------------------------- ### Alternative Matching Function (DFA) Example Source: https://github.com/pcre2project/pcre2/blob/main/doc/pcre2test.txt Illustrates the output of the pcre2_dfa_match() function, which lists all matches starting at the first point of a match, prioritizing the longest match. ```bash re> /(tang|tangerine|tan)/ data> yellow tangerine\=dfa 0: tangerine 1: tang 2: tan ``` -------------------------------- ### PCRE2 Character Class Example Source: https://github.com/pcre2project/pcre2/blob/main/doc/pcre2.txt Demonstrates a basic character class matching lowercase English vowels. A circumflex at the start negates the class, matching any character not in the set. ```regex [aeiou] ``` ```regex [^aeiou] ``` -------------------------------- ### pcre2test Example: Multi-Segment Matching with Offset Source: https://github.com/pcre2project/pcre2/blob/main/doc/pcre2.txt Illustrates multi-segment matching with PCRE2_PARTIAL_HARD and the 'offset' modifier. Shows how to resume matching after a partial match by specifying the start offset. ```regex re> /\d?\d(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\d\d/ data> ...the date is 23ja=ph Partial match: 23ja data> ...the date is 23jan19 and on that day=offset=15 0: 23jan19 1: jan ``` -------------------------------- ### Interactive pcre2test Session Example Source: https://github.com/pcre2project/pcre2/blob/main/doc/html/pcre2test.html Demonstrates an interactive session with pcre2test, showing pattern matching and substring capture. ```bash $ pcre2test PCRE2 version 10.22 2016-07-29 re> /^abc(\d+)/ data> abc123 0: abc123 1: 123 data> xyz No match ``` -------------------------------- ### (*COMMIT) Example in PCRE2 Source: https://github.com/pcre2project/pcre2/blob/main/doc/pcre2.txt Illustrates the use of (*COMMIT) to prevent backtracking past a certain point, effectively anchoring the match to the current starting position. This is useful for performance optimizations. ```regex a+(*COMMIT)b ``` -------------------------------- ### PCRE2 Forward Relative Backreference Example Source: https://github.com/pcre2project/pcre2/blob/main/doc/html/pcre2pattern.html Illustrates the use of \g{+1} for forward relative backreferences, referring to the next capture group to be started. This is useful for patterns that repeat. ```regex (sens|respons)e and \1ibility ``` -------------------------------- ### pcre2test String Callout Example Source: https://github.com/pcre2project/pcre2/blob/main/doc/html/pcre2test.html Demonstrates callouts with string arguments, showing the callout string and its offset before reflecting the subject string for each callout. ```text re> /^ab(?C'first')cd(?C"second")ef/ data> abcdefg Callout (7): 'first' --->abcdefg ^ ^ c Callout (20): "second" --->abcdefg ^ ^ e 0: abcdef ``` -------------------------------- ### Find Word Containing 'rh' Not at Start (PCRE2) Source: https://github.com/pcre2project/pcre2/blob/main/doc/pcre2.txt This example uses a scan substring assertion to find a word that contains the sequence 'rh' but not at the beginning of the word. The word is captured in group 1. ```regex \b(\w++)(*scs:(1).+rh) ``` -------------------------------- ### PCRE2 (*COMMIT) Verb Example Source: https://github.com/pcre2project/pcre2/blob/main/doc/pcre2.txt Demonstrates the use of the (*COMMIT) verb to prevent backtracking. The optimization skips to the first character of the pattern, and (*COMMIT) ensures that if a match fails at this point, no other starting points are tried. ```regex re> /(*COMMIT)abc/no_start_optimize data> xyzabc No match ``` -------------------------------- ### Run PCRE2 Demo Program (Case-Insensitive Match) Source: https://github.com/pcre2project/pcre2/blob/main/doc/html/pcre2sample.html Example of running the pcre2demo program with the -i option for a case-insensitive match. ```bash ./pcre2demo -i 'cat' 'the dog sat on the CAT' ``` -------------------------------- ### Configuration: Callbacks and Tables Source: https://github.com/pcre2project/pcre2/blob/main/doc/html/index.html Functions for setting up callout functions and character tables. ```APIDOC ## pcre2_set_callout ### Description Set up a callout function ## pcre2_set_character_tables ### Description Set character tables ## pcre2_set_substitute_callout ### Description Set a substitution callout function ## pcre2_set_substitute_case_callout ### Description Set a substitution case callout function ``` -------------------------------- ### PCRE2 SKIP Verb Example Source: https://github.com/pcre2project/pcre2/blob/main/doc/html/pcre2pattern.html The \*SKIP verb causes a match failure and advances the starting position to where \*SKIP was encountered, rather than the next character. This signifies that the text matched before \*SKIP cannot be part of a successful match. ```regex a+(\*SKIP)b ```