PCRE_EXEC(3)
π PCRE_EXEC(3) π
okay bestie, gather round, because this is the pcre_exec page from the Library Functions Manual and it's literally the moment your regex stops being a vibe and starts being results. π
β¨ NAME β¨
PCRE β Perl-compatible regular expressions. yes that's it. that's the whole name. iconic, terse, no notes. π
πΈ SYNOPSIS πΈ
#include <pcre.h>
int pcre_exec(const pcre *code, const pcre_extra *extra,
const char *subject, int length, int startoffset,
int options, int *ovector, int ovecsize);
int pcre16_exec(const pcre16 *code, const pcre16_extra *extra,
PCRE_SPTR16 subject, int length, int startoffset,
int options, int *ovector, int ovecsize);
int pcre32_exec(const pcre32 *code, const pcre32_extra *extra,
PCRE_SPTR32 subject, int length, int startoffset,
int options, int *ovector, int ovecsize);
three flavors, one vibe: 8-bit, 16-bit, 32-bit. pick your fighter. β
π¦ DESCRIPTION π¦
So what does this function do, you ask? It takes a compiled regular expression and matches it against a subject string you hand it, using a matching algorithm that's similar to Perl's. Then it returns offsets to the captured substrings, aka the receipts, aka the exact positions where your regex popped off. π
Here's the whole cast of arguments, one by one:
codeβ points to the compiled pattern. the main character. πextraβ points to an associatedpcre[16|32]_extrastructure, or isNULLif you're going minimal.subjectβ points to the subject string. the thing you're matching against.lengthβ length of the subject string.startoffsetβ offset in the subject at which to start matching. like, don't start at the beginning, start here. β¨optionsβ option bits.ovectorβ points to a vector of ints for result offsets.ovecsizeβ number of elements in the vector (a multiple of 3).
Quick unit check before we move on, because this matters: the units for length and startoffset are bytes for pcre_exec(), 16-bit data items for pcre16_exec(), and 32-bit items for pcre32_exec(). don't mix those up, i'm begging. π
π the options (all of them, no skipping, periodt)
PCRE_ANCHOREDβ match only at the first position.PCRE_BSR_ANYCRLFβ\Rmatches only CR, LF, or CRLF.PCRE_BSR_UNICODEβ\Rmatches all Unicode line endings.PCRE_NEWLINE_ANYβ recognize any Unicode newline sequence.PCRE_NEWLINE_ANYCRLFβ recognize CR, LF, & CRLF as newline sequences.PCRE_NEWLINE_CRβ recognize CR as the only newline sequence.PCRE_NEWLINE_CRLFβ recognize CRLF as the only newline sequence.PCRE_NEWLINE_LFβ recognize LF as the only newline sequence.PCRE_NOTBOLβ subject string is not the beginning of a line.PCRE_NOTEOLβ subject string is not the end of a line.PCRE_NOTEMPTYβ an empty string is not a valid match.PCRE_NOTEMPTY_ATSTARTβ an empty string at the start of the subject is not a valid match.PCRE_NO_START_OPTIMIZEβ do not do "start-match" optimizations.PCRE_NO_UTF16_CHECKβ do not check the subject for UTF-16 validity (only relevant ifPCRE_UTF16was set at compile time).PCRE_NO_UTF32_CHECKβ do not check the subject for UTF-32 validity (only relevant ifPCRE_UTF32was set at compile time).PCRE_NO_UTF8_CHECKβ do not check the subject for UTF-8 validity (only relevant ifPCRE_UTF8was set at compile time).PCRE_PARTIALβ returnPCRE_ERROR_PARTIALfor a partial match if no full matches are found.PCRE_PARTIAL_SOFTβ returnPCRE_ERROR_PARTIALfor a partial match if no full matches are found.PCRE_PARTIAL_HARDβ returnPCRE_ERROR_PARTIALfor a partial match if that is found before a full match.
ngl, notice howPCRE_PARTIALandPCRE_PARTIAL_SOFTshare that description in the original? that's not a typo on my part, that's the actual grouping. keep them together, bestie. π«Ά
For the details of partial matching, go read the pcrepartial page. π§
π the pcre_extra structure, field by field
A pcre_extra structure contains these fields:
flagsβ bits indicating which fields are set.study_dataβ opaque data frompcre[16|32]_study().match_limitβ limit on internal resource use.match_limit_recursionβ limit on internal recursion depth.callout_dataβ opaque data passed back to callouts.tablesβ points to character tables or isNULL.markβ for passing back a*MARKpointer.executable_jitβ opaque data from JIT compilation.
and the flag bits themselves are: PCRE_EXTRA_STUDY_DATA, PCRE_EXTRA_MATCH_LIMIT, PCRE_EXTRA_MATCH_LIMIT_RECURSION, PCRE_EXTRA_CALLOUT_DATA, PCRE_EXTRA_TABLES, PCRE_EXTRA_MARK and PCRE_EXTRA_EXECUTABLE_JIT. π
There's a complete description of the PCRE native API in the pcreapi page, and a description of the POSIX API in the pcreposix page. go read those too, i'm not gonna gatekeep. π
and that's the whole page, bestie β written for PCRE 8.33, dated 12 May 2013. yeah, it's giving vintage, but the knowledge is timeless and so are you. slay, compile safely, and may all your matches be full ones. πβ¨ππ¦πΈπ
π¦ where this page comes from
- alpine 3.22: pcre-doc 8.45-r4 Β· BSD-3-Clause
- alpine 3.23: pcre-doc 8.45-r4 Β· BSD-3-Clause
- alpine 3.24: pcre-doc 8.45-r4 Β· BSD-3-Clause
- arch rolling: pcre 8.45-5 Β· BSD
- centos-stream 9: pcre-devel 8.44-4.el9 Β· BSD
- centos-stream 9: pcre-devel 8.44-4.el9 Β· BSD
- debian bookworm: libpcre3-dev 2:8.39-15
- fedora 43: pcre-devel 8.45-1.fc43.9 Β· BSD-3-Clause
- fedora 43: pcre-devel 8.45-1.fc43.9 Β· BSD-3-Clause
- fedora 44: pcre-devel 8.45-1.fc43.9 Β· BSD-3-Clause
- fedora 44: pcre-devel 8.45-1.fc43.9 Β· BSD-3-Clause
- nixos 26.05: pcre 8.45
- nixos 26.05: pcre-cpp 8.45
- opensuse-leap 16.0: pcre-devel 8.45-160000.2.2 Β· BSD-3-Clause
- opensuse-leap 16.0: pcre-devel 8.45-160000.2.2 Β· BSD-3-Clause
- opensuse-leap 16.0: pcre-devel 8.45-160000.2.2 Β· BSD-3-Clause
- opensuse-leap 16.0: pcre-devel 8.45-160000.2.2 Β· BSD-3-Clause
- ubuntu jammy: libpcre3-dev 2:8.39-13build5
- ubuntu jammy: libpcre3-dev 2:8.39-13ubuntu0.22.04.1
- ubuntu noble: libpcre3-dev 2:8.39-15build1
- ubuntu questing: libpcre3-dev 2:8.39-15.1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms