1PCRE2POSIX(3)              Library Functions Manual              PCRE2POSIX(3)
2
3
4

NAME

6       PCRE2 - Perl-compatible regular expressions (revised API)
7

SYNOPSIS

9
10       #include <pcre2posix.h>
11
12       int regcomp(regex_t *preg, const char *pattern,
13            int cflags);
14
15       int regexec(const regex_t *preg, const char *string,
16            size_t nmatch, regmatch_t pmatch[], int eflags);
17
18       size_t regerror(int errcode, const regex_t *preg,
19            char *errbuf, size_t errbuf_size);
20
21       void regfree(regex_t *preg);
22

DESCRIPTION

24
25       This  set of functions provides a POSIX-style API for the PCRE2 regular
26       expression 8-bit library. See the pcre2api documentation for a descrip‐
27       tion  of PCRE2's native API, which contains much additional functional‐
28       ity. There are no POSIX-style wrappers for PCRE2's  16-bit  and  32-bit
29       libraries.
30
31       The functions described here are just wrapper functions that ultimately
32       call the  PCRE2  native  API.  Their  prototypes  are  defined  in  the
33       pcre2posix.h  header  file,  and  on Unix systems the library itself is
34       called libpcre2-posix.a, so can be accessed by adding -lpcre2-posix  to
35       the  command  for  linking  an  application that uses them. Because the
36       POSIX functions call the native ones,  it  is  also  necessary  to  add
37       -lpcre2-8.
38
39       Those  POSIX  option bits that can reasonably be mapped to PCRE2 native
40       options have been implemented. In addition, the option REG_EXTENDED  is
41       defined  with  the  value  zero. This has no effect, but since programs
42       that are written to the POSIX interface often use  it,  this  makes  it
43       easier  to  slot in PCRE2 as a replacement library. Other POSIX options
44       are not even defined.
45
46       There are also some options that are not defined by POSIX.  These  have
47       been  added  at  the  request  of users who want to make use of certain
48       PCRE2-specific features via the POSIX calling interface or to  add  BSD
49       or GNU functionality.
50
51       When  PCRE2  is  called via these functions, it is only the API that is
52       POSIX-like in style. The syntax and semantics of  the  regular  expres‐
53       sions  themselves  are  still  those of Perl, subject to the setting of
54       various PCRE2 options, as described below. "POSIX-like in style"  means
55       that  the  API  approximates  to  the POSIX definition; it is not fully
56       POSIX-compatible, and in multi-unit encoding  domains  it  is  probably
57       even less compatible.
58
59       The header for these functions is supplied as pcre2posix.h to avoid any
60       potential clash with other POSIX  libraries.  It  can,  of  course,  be
61       renamed or aliased as regex.h, which is the "correct" name. It provides
62       two structure types, regex_t for  compiled  internal  forms,  and  reg‐
63       match_t  for  returning  captured substrings. It also defines some con‐
64       stants whose names start  with  "REG_";  these  are  used  for  setting
65       options and identifying error codes.
66

COMPILING A PATTERN

68
69       The  function regcomp() is called to compile a pattern into an internal
70       form. By default, the pattern is a C string terminated by a binary zero
71       (but  see  REG_PEND below). The preg argument is a pointer to a regex_t
72       structure that is used as a base for storing information about the com‐
73       piled  regular  expression. (It is also used for input when REG_PEND is
74       set.)
75
76       The argument cflags is either zero, or contains one or more of the bits
77       defined by the following macros:
78
79         REG_DOTALL
80
81       The  PCRE2_DOTALL  option  is set when the regular expression is passed
82       for compilation to the native function. Note  that  REG_DOTALL  is  not
83       part of the POSIX standard.
84
85         REG_ICASE
86
87       The  PCRE2_CASELESS option is set when the regular expression is passed
88       for compilation to the native function.
89
90         REG_NEWLINE
91
92       The PCRE2_MULTILINE option is set when the regular expression is passed
93       for  compilation  to the native function. Note that this does not mimic
94       the defined POSIX behaviour for REG_NEWLINE  (see  the  following  sec‐
95       tion).
96
97         REG_NOSPEC
98
99       The  PCRE2_LITERAL  option is set when the regular expression is passed
100       for compilation to the native function. This disables all meta  charac‐
101       ters  in the pattern, causing it to be treated as a literal string. The
102       only other options that are  allowed  with  REG_NOSPEC  are  REG_ICASE,
103       REG_NOSUB,  REG_PEND,  and REG_UTF. Note that REG_NOSPEC is not part of
104       the POSIX standard.
105
106         REG_NOSUB
107
108       When a pattern that is compiled with this flag is passed  to  regexec()
109       for  matching, the nmatch and pmatch arguments are ignored, and no cap‐
110       tured strings are returned. Versions of the PCRE library prior to 10.22
111       used  to  set  the  PCRE2_NO_AUTO_CAPTURE  compile  option, but this no
112       longer happens because it disables the use of backreferences.
113
114         REG_PEND
115
116       If this option is set, the reg_endp field in the preg structure  (which
117       has the type const char *) must be set to point to the character beyond
118       the end of the pattern before calling regcomp(). The pattern itself may
119       now contain binary zeros, which are treated as data characters. Without
120       REG_PEND, a binary zero terminates the pattern and the re_endp field is
121       ignored.  This  is  a GNU extension to the POSIX standard and should be
122       used with caution in software intended to be portable to other systems.
123
124         REG_UCP
125
126       The PCRE2_UCP option is set when the regular expression is  passed  for
127       compilation  to  the  native function. This causes PCRE2 to use Unicode
128       properties when matchine \d, \w,  etc.,  instead  of  just  recognizing
129       ASCII values. Note that REG_UCP is not part of the POSIX standard.
130
131         REG_UNGREEDY
132
133       The  PCRE2_UNGREEDY option is set when the regular expression is passed
134       for compilation to the native function. Note that REG_UNGREEDY  is  not
135       part of the POSIX standard.
136
137         REG_UTF
138
139       The  PCRE2_UTF  option is set when the regular expression is passed for
140       compilation to the native function. This causes the pattern itself  and
141       all  data  strings used for matching it to be treated as UTF-8 strings.
142       Note that REG_UTF is not part of the POSIX standard.
143
144       In the absence of these flags, no options  are  passed  to  the  native
145       function.   This  means  the  the  regex is compiled with PCRE2 default
146       semantics. In particular, the way it handles newline characters in  the
147       subject  string  is  the Perl way, not the POSIX way. Note that setting
148       PCRE2_MULTILINE has only some of the effects specified for REG_NEWLINE.
149       It  does not affect the way newlines are matched by the dot metacharac‐
150       ter (they are not) or by a negative class such as [^a] (they are).
151
152       The yield of regcomp() is zero on success, and non-zero otherwise.  The
153       preg  structure  is  filled  in on success, and one other member of the
154       structure (as well as re_endp) is public: re_nsub contains  the  number
155       of capturing subpatterns in the regular expression. Various error codes
156       are defined in the header file.
157
158       NOTE: If the yield of regcomp() is non-zero, you must  not  attempt  to
159       use the contents of the preg structure. If, for example, you pass it to
160       regexec(), the result is undefined and your program is likely to crash.
161

MATCHING NEWLINE CHARACTERS

163
164       This area is not simple, because POSIX and Perl take different views of
165       things.   It  is not possible to get PCRE2 to obey POSIX semantics, but
166       then PCRE2 was never intended to be a POSIX engine. The following table
167       lists  the  different  possibilities for matching newline characters in
168       Perl and PCRE2:
169
170                                 Default   Change with
171
172         . matches newline          no     PCRE2_DOTALL
173         newline matches [^a]       yes    not changeable
174         $ matches \n at end        yes    PCRE2_DOLLAR_ENDONLY
175         $ matches \n in middle     no     PCRE2_MULTILINE
176         ^ matches \n in middle     no     PCRE2_MULTILINE
177
178       This is the equivalent table for a POSIX-compatible pattern matcher:
179
180                                 Default   Change with
181
182         . matches newline          yes    REG_NEWLINE
183         newline matches [^a]       yes    REG_NEWLINE
184         $ matches \n at end        no     REG_NEWLINE
185         $ matches \n in middle     no     REG_NEWLINE
186         ^ matches \n in middle     no     REG_NEWLINE
187
188       This behaviour is not what happens when PCRE2 is called via  its  POSIX
189       API.  By  default, PCRE2's behaviour is the same as Perl's, except that
190       there is no equivalent for PCRE2_DOLLAR_ENDONLY in Perl. In both  PCRE2
191       and Perl, there is no way to stop newline from matching [^a].
192
193       Default  POSIX newline handling can be obtained by setting PCRE2_DOTALL
194       and PCRE2_DOLLAR_ENDONLY when  calling  pcre2_compile()  directly,  but
195       there  is  no  way  to make PCRE2 behave exactly as for the REG_NEWLINE
196       action. When using the POSIX API, passing REG_NEWLINE to  PCRE2's  reg‐
197       comp() function causes PCRE2_MULTILINE to be passed to pcre2_compile(),
198       and REG_DOTALL passes PCRE2_DOTALL. There is no way to pass  PCRE2_DOL‐
199       LAR_ENDONLY.
200

MATCHING A PATTERN

202
203       The  function  regexec()  is  called  to  match a compiled pattern preg
204       against a given string, which is by default terminated by a  zero  byte
205       (but  see  REG_STARTEND below), subject to the options in eflags. These
206       can be:
207
208         REG_NOTBOL
209
210       The PCRE2_NOTBOL option is set when calling the underlying PCRE2 match‐
211       ing function.
212
213         REG_NOTEMPTY
214
215       The  PCRE2_NOTEMPTY  option  is  set  when calling the underlying PCRE2
216       matching function. Note that REG_NOTEMPTY is  not  part  of  the  POSIX
217       standard.  However, setting this option can give more POSIX-like behav‐
218       iour in some situations.
219
220         REG_NOTEOL
221
222       The PCRE2_NOTEOL option is set when calling the underlying PCRE2 match‐
223       ing function.
224
225         REG_STARTEND
226
227       When  this  option  is  set,  the  subject  string  starts  at string +
228       pmatch[0].rm_so and ends at  string  +  pmatch[0].rm_eo,  which  should
229       point  to  the  first  character beyond the string. There may be binary
230       zeros within the subject string, and indeed, using REG_STARTEND is  the
231       only way to pass a subject string that contains a binary zero.
232
233       Whatever  the  value  of  pmatch[0].rm_so,  the  offsets of the matched
234       string and any captured substrings are  still  given  relative  to  the
235       start  of  string  itself. (Before PCRE2 release 10.30 these were given
236       relative to string +  pmatch[0].rm_so,  but  this  differs  from  other
237       implementations.)
238
239       This  is  a  BSD  extension,  compatible with but not specified by IEEE
240       Standard 1003.2 (POSIX.2), and should be used with caution in  software
241       intended  to  be  portable to other systems. Note that a non-zero rm_so
242       does not imply REG_NOTBOL; REG_STARTEND affects only the  location  and
243       length  of  the string, not how it is matched. Setting REG_STARTEND and
244       passing pmatch as NULL are mutually exclusive; the error REG_INVARG  is
245       returned.
246
247       If  the pattern was compiled with the REG_NOSUB flag, no data about any
248       matched strings  is  returned.  The  nmatch  and  pmatch  arguments  of
249       regexec() are ignored (except possibly as input for REG_STARTEND).
250
251       The  value  of  nmatch  may  be  zero, and the value pmatch may be NULL
252       (unless REG_STARTEND is set); in both these cases  no  data  about  any
253       matched strings is returned.
254
255       Otherwise,  the  portion  of  the string that was matched, and also any
256       captured substrings, are returned via the pmatch argument, which points
257       to  an  array  of  nmatch structures of type regmatch_t, containing the
258       members rm_so and rm_eo. These contain the byte  offset  to  the  first
259       character of each substring and the offset to the first character after
260       the end of each substring, respectively. The 0th element of the  vector
261       relates  to  the  entire portion of string that was matched; subsequent
262       elements relate to the capturing subpatterns of the regular expression.
263       Unused entries in the array have both structure members set to -1.
264
265       A  successful  match  yields  a  zero  return;  various error codes are
266       defined in the header file, of  which  REG_NOMATCH  is  the  "expected"
267       failure code.
268

ERROR MESSAGES

270
271       The regerror() function maps a non-zero errorcode from either regcomp()
272       or regexec() to a printable message. If preg is  not  NULL,  the  error
273       should have arisen from the use of that structure. A message terminated
274       by a binary zero is placed in errbuf. If the buffer is too short,  only
275       the first errbuf_size - 1 characters of the error message are used. The
276       yield of the function is the size of buffer needed to  hold  the  whole
277       message,  including  the  terminating  zero. This value is greater than
278       errbuf_size if the message was truncated.
279

MEMORY USAGE

281
282       Compiling a regular expression causes memory to be allocated and  asso‐
283       ciated  with  the preg structure. The function regfree() frees all such
284       memory, after which preg may no longer be used as  a  compiled  expres‐
285       sion.
286

AUTHOR

288
289       Philip Hazel
290       University Computing Service
291       Cambridge, England.
292

REVISION

294
295       Last updated: 15 June 2017
296       Copyright (c) 1997-2017 University of Cambridge.
297
298
299
300PCRE2 10.30                      15 June 2017                    PCRE2POSIX(3)
Impressum