PCRE2_SUBSTITUTE(3)
π PCRE2_SUBSTITUTE(3) β Library Functions Manual π
okay bestie, this is the PCRE2_SUBSTITUTE(3) page from the Library Functions Manual β the function that matches a compiled regex against a subject and then does the substitution glow-up. β¨
πΈ NAME πΈ
PCRE2 β Perl-compatible regular expressions (revised API) π
β¨ SYNOPSIS β¨
#include <pcre2.h>
int pcre2_substitute(const pcre2_code *code, PCRE2_SPTR subject,
PCRE2_SIZE length, PCRE2_SIZE startoffset,
uint32_t options, pcre2_match_data *match_data,
pcre2_match_context *mcontext, PCRE2_SPTR replacement,
PCRE2_SIZE rlength, PCRE2_UCHAR *outputbuffer,
PCRE2_SIZE *outlengthptr);
π¦ DESCRIPTION π¦
This function matches a compiled regular expression against a given subject string, using a matching algorithm that is similar to Perl's. Then it makes a copy of the subject and substitutes a replacement string for what was matched. π
the arguments, fr fr π«Ά
codeβ points to the compiled pattern.subjectβ points to the subject string.lengthβ length of the subject string.startoffsetβ offset in the subject at which to start matching.optionsβ option bits.match_dataβ points to a match data block, or isNULL.mcontextβ points to a match context, or isNULL.replacementβ points to the replacement string.rlengthβ length of the replacement string.outputbufferβ points to the output buffer.outlengthptrβ points to the length of the output buffer.
A match data block is needed only if you want to inspect the data from the final match that is returned in that block, or if PCRE2_SUBSTITUTE_MATCHED is set. π«Ά
A match context is needed only if you want to:
- 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.
code units + output length β important!! π
The length, startoffset, and rlength values are code units, not characters β same for the contents of the variable pointed at by outlengthptr. That variable must contain the length of the output buffer when the function is called. If the function is successful, the value is changed to the length of the new string, excluding the trailing zero that is automatically added. π
The subject and replacement lengths can be given as PCRE2_ZERO_TERMINATED for zero-terminated strings. The options are:
PCRE2_ANCHOREDβ match only at the first position.PCRE2_ENDANCHOREDβ match only at end of subject.PCRE2_NOTBOLβ subject is not the beginning of a line.PCRE2_NOTEOLβ subject is not the end of a line.PCRE2_NOTEMPTYβ an empty string is not a valid match.PCRE2_NOTEMPTY_ATSTARTβ an empty string at the start of the subject is not a valid match.PCRE2_NO_JITβ do not use JIT matching.PCRE2_NO_UTF_CHECKβ do not check for UTF validity in the subject or replacement (only relevant ifPCRE2_UTFwas set at compile time).PCRE2_SUBSTITUTE_EXTENDEDβ do extended replacement processing.PCRE2_SUBSTITUTE_GLOBALβ replace all occurrences in the subject.PCRE2_SUBSTITUTE_LITERALβ the replacement string is literal.PCRE2_SUBSTITUTE_MATCHEDβ use pre-existing match data for first match.PCRE2_SUBSTITUTE_OVERFLOW_LENGTHβ if overflow, compute needed length.PCRE2_SUBSTITUTE_REPLACEMENT_ONLYβ return only replacement string(s).PCRE2_SUBSTITUTE_UNKNOWN_UNSETβ treat unknown group as unset.PCRE2_SUBSTITUTE_UNSET_EMPTYβ simple unset insert = empty string.
If PCRE2_SUBSTITUTE_LITERAL is set, PCRE2_SUBSTITUTE_EXTENDED, PCRE2_SUBSTITUTE_UNKNOWN_UNSET, and PCRE2_SUBSTITUTE_UNSET_EMPTY are ignored. π¨
β οΈ IfPCRE2_SUBSTITUTE_MATCHEDis set,match_datamust be non-NULL; its contents must be the result of a call topcre2_match()(orpcre2_jit_match()) using the same pattern, subject pointer, effective subject length, start offset, and match options.
The function returns the number of substitutions, which may be zero if there are no matches. The result may be greater than one only when PCRE2_SUBSTITUTE_GLOBAL is set. In the event of an error, a negative error code is returned. π
There is a complete description of the PCRE2 native API in the pcre2api page and a description of the POSIX API in the pcre2posix page. π¦
mwah π β PCRE2 10.47, 03 October 2025, PCRE2_SUBSTITUTE(3).
π¦ where this page comes from
- fedora 43: pcre2-devel 10.47-1.fc43 Β· BSD-3-Clause AND FSFULLR AND X11 AND GPL-2.0-or-later AND FSFAP AND FSFUL AND GPL-3.0-or-later
- fedora 43: pcre2-devel 10.47-1.fc43 Β· BSD-3-Clause AND FSFULLR AND X11 AND GPL-2.0-or-later AND FSFAP AND FSFUL AND GPL-3.0-or-later
- fedora 44: pcre2-devel 10.47-1.fc44.1 Β· BSD-3-Clause AND FSFULLR AND X11 AND GPL-2.0-or-later AND FSFAP AND FSFUL AND GPL-3.0-or-later
- fedora 44: pcre2-devel 10.47-1.fc44.1 Β· BSD-3-Clause AND FSFULLR AND X11 AND GPL-2.0-or-later AND FSFAP AND FSFUL AND GPL-3.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms