PCREPOSIX(3)
β¨ NAME β¨
Bestie, this is PCREPOSIX(3) β the Library Functions Manual page for PCRE, which stands for Perl-compatible regular expressions. π And this particular page? It's the POSIX-flavored wrapper era of it. Periodt.
πΈ SYNOPSIS πΈ
#include <pcreposix.h>
int regcomp(regex_t *preg, const char *pattern,
int cflags);
int regexec(regex_t *preg, const char *string,
size_t nmatch, regmatch_t pmatch[], int eflags);
size_t regerror(int errcode, const regex_t *preg,
char *errbuf, size_t errbuf_size);
void regfree(regex_t *preg);
π DESCRIPTION π
Okay so, this whole little squad of functions hands you a POSIX-style API for the PCRE regular expression 8-bit library. π¦ Want the tea on PCRE's native API, which has like, way more functionality? That's in the pcreapi documentation, go look. And heads up: there is no POSIX-style wrapper for PCRE's 16-bit and 32-bit library. None. Not a thing. π
The functions here are literally just wrapper functions that ultimately call the PCRE native API. Their prototypes live in the pcreposix.h header file, and on Unix systems the library itself is called pcreposix.a, so you can reach it by adding -lpcreposix to the command for linking an application that uses them. And because the POSIX functions call the native ones, you also gotta add -lpcre. Both. Don't skip it. π
I have implemented only those POSIX option bits that can be reasonably mapped to PCRE native options. Also, the option REG_EXTENDED is defined with the value zero. It has no effect, but since programs written to the POSIX interface often use it, this makes it easier to slot in PCRE as a replacement library. Other POSIX options aren't even defined. Iconic laziness, honestly.
There are also some other options that are not defined by POSIX. These got added at the request of users who wanted to use certain PCRE-specific features via the POSIX calling interface. π«Ά
When PCRE is called via these functions, it's only the API that's POSIX-like in style. The syntax and semantics of the regular expressions themselves are still those of Perl, subject to the setting of various PCRE options, as described below. "POSIX-like in style" means the API approximates the POSIX definition; it is not fully POSIX-compatible, and in multi-byte encoding domains it is probably even less compatible. So like, vibe-adjacent to POSIX. Not POSIX. π§
The header for these functions is supplied as pcreposix.h to avoid any potential clash with other POSIX libraries. It can, of course, be renamed or aliased as regex.h, which is the "correct" name. It provides two structure types: regex_t for compiled internal forms, and regmatch_t for returning captured substrings. It also defines some constants whose names start with "REG_"; these are used for setting options and identifying error codes. π
π₯ COMPILING A PATTERN π₯
The function regcomp() is called to compile a pattern into an internal form. The pattern is a C string terminated by a binary zero, and is passed in the argument pattern. The preg argument is a pointer to a regex_t structure that is used as a base for storing information about the compiled regular expression.
The argument cflags is either zero, or contains one or more of the bits defined by the following macros:
REG_DOTALLβ thePCRE_DOTALLoption is set when the regular expression is passed for compilation to the native function. Note thatREG_DOTALLis not part of the POSIX standard. πREG_ICASEβ thePCRE_CASELESSoption is set when the regular expression is passed for compilation to the native function.REG_NEWLINEβ thePCRE_MULTILINEoption is set when the regular expression is passed for compilation to the native function. Note that this does not mimic the defined POSIX behaviour forREG_NEWLINE(see the following section). πREG_NOSUBβ thePCRE_NO_AUTO_CAPTUREoption is set when the regular expression is passed for compilation to the native function. In addition, when a pattern that is compiled with this flag is passed toregexec()for matching, the nmatch and pmatch arguments are ignored, and no captured strings are returned.REG_UCPβ thePCRE_UCPoption is set when the regular expression is passed for compilation to the native function. This causes PCRE to use Unicode properties when matching\d,\w, etc., instead of just recognizing ASCII values. Note thatREG_UTF8is not part of the POSIX standard.REG_UNGREEDYβ thePCRE_UNGREEDYoption is set when the regular expression is passed for compilation to the native function. Note thatREG_UNGREEDYis not part of the POSIX standard.REG_UTF8β thePCRE_UTF8option is set when the regular expression is passed for compilation to the native function. This causes the pattern itself and all data strings used for matching it to be treated as UTF-8 strings. Note thatREG_UTF8is not part of the POSIX standard.
In the absence of these flags, no options are passed to the native function. This means the regex is compiled with PCRE default semantics. In particular, the way it handles newline characters in the subject string is the Perl way, not the POSIX way. Note that setting PCRE_MULTILINE has only some of the effects specified for REG_NEWLINE. It does not affect the way newlines are matched by . (they are not) or by a negative class such as [^a] (they are). π
The yield of regcomp() is zero on success, and non-zero otherwise. The preg structure is filled in on success, and one member of the structure is public: re_nsub contains the number of capturing subpatterns in the regular expression. Various error codes are defined in the header file.
NOTE: If the yield ofregcomp()is non-zero, you must not attempt to use the contents of the preg structure. If, for example, you pass it toregexec(), the result is undefined and your program is likely to crash. Not a drill, bestie. π
π MATCHING NEWLINE CHARACTERS π
This area is not simple, because POSIX and Perl take different views of things. It is not possible to get PCRE to obey POSIX semantics, but then PCRE was never intended to be a POSIX engine. The following list has all the different possibilities for matching newline characters in PCRE:
.matches newline β Default:no; Change with:PCRE_DOTALL- newline matches
[^a]β Default:yes; Change with:not changeable $matches\nat end β Default:yes; Change with:PCRE_DOLLARENDONLY$matches\nin middle β Default:no; Change with:PCRE_MULTILINE^matches\nin middle β Default:no; Change with:PCRE_MULTILINE
And this is the equivalent list for POSIX:
.matches newline β Default:yes; Change with:REG_NEWLINE- newline matches
[^a]β Default:yes; Change with:REG_NEWLINE $matches\nat end β Default:no; Change with:REG_NEWLINE$matches\nin middle β Default:no; Change with:REG_NEWLINE^matches\nin middle β Default:no; Change with:REG_NEWLINE
PCRE's behaviour is the same as Perl's, except that there is no equivalent for PCRE_DOLLAR_ENDONLY in Perl. In both PCRE and Perl, there is no way to stop newline from matching [^a]. π€
The default POSIX newline handling can be obtained by setting PCRE_DOTALL and PCRE_DOLLAR_ENDONLY, but there is no way to make PCRE behave exactly as for the REG_NEWLINE action.
π MATCHING A PATTERN π
The function regexec() is called to match a compiled pattern preg against a given string, which is by default terminated by a zero byte (but see REG_STARTEND below), subject to the options in eflags. These can be:
REG_NOTBOLβ thePCRE_NOTBOLoption is set when calling the underlying PCRE matching function.REG_NOTEMPTYβ thePCRE_NOTEMPTYoption is set when calling the underlying PCRE matching function. Note thatREG_NOTEMPTYis not part of the POSIX standard. However, setting this option can give more POSIX-like behaviour in some situations.REG_NOTEOLβ thePCRE_NOTEOLoption is set when calling the underlying PCRE matching function.REG_STARTENDβ the string is considered to start atstring + pmatch[0].rm_soand to have a terminatingNULlocated atstring + pmatch[0].rm_eo(there need not actually be aNULat that location), regardless of the value of nmatch. This is a BSD extension, compatible with but not specified by IEEE Standard 1003.2 (POSIX.2), and should be used with caution in software intended to be portable to other systems. Note that a non-zerorm_sodoes not implyREG_NOTBOL;REG_STARTENDaffects only the location of the string, not how it is matched.
If the pattern was compiled with the REG_NOSUB flag, no data about any matched strings is returned. The nmatch and pmatch arguments of regexec() are ignored.
If the value of nmatch is zero, or if the value pmatch is NULL, no data about any matched strings is returned.
Otherwise, the portion of the string that was matched, and also any captured substrings, are returned via the pmatch argument, which points to an array of nmatch structures of type regmatch_t, containing the members rm_so and rm_eo. These contain the offset to the first character of each substring and the offset to the first character after the end of each substring, respectively. The 0th element of the vector relates to the entire portion of string that was matched; subsequent elements relate to the capturing subpatterns of the regular expression. Unused entries in the array have both structure members set to -1.
A successful match yields a zero return; various error codes are defined in the header file, of which REG_NOMATCH is the "expected" failure code. π«Ά
π ERROR MESSAGES π
The regerror() function maps a non-zero errorcode from either regcomp() or regexec() to a printable message. If preg is not NULL, the error should have arisen from the use of that structure. A message terminated by a binary zero is placed in errbuf. The length of the message, including the zero, is limited to errbuf_size. The yield of the function is the size of buffer needed to hold the whole message. π
π MEMORY USAGE π
Compiling a regular expression causes memory to be allocated and associated with the preg structure. The function regfree() frees all such memory, after which preg may no longer be used as a compiled expression. Free it. Please. π
π AUTHOR π
Philip Hazel University Computing Service Cambridge CB2 3QH, England.
β REVISION β
Last updated: 09 January 2012 Copyright (c) 1997-2012 University of Cambridge.
And that's the whole page, bestie β this was PCREPOSIX(3) from PCRE 8.30, dated 09 January 2012, and yes, the copyright is all University of Cambridge, 1997 through 2012. Stay POSIX-adjacent, hydrate, and always regfree() your patterns. π
β¨π¦
π¦ 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