1REGCOMP(3P)                POSIX Programmer's Manual               REGCOMP(3P)
2
3
4

PROLOG

6       This  manual  page is part of the POSIX Programmer's Manual.  The Linux
7       implementation of this interface may differ (consult the  corresponding
8       Linux  manual page for details of Linux behavior), or the interface may
9       not be implemented on Linux.
10

NAME

12       regcomp, regerror, regexec, regfree — regular expression matching
13

SYNOPSIS

15       #include <regex.h>
16
17       int regcomp(regex_t *restrict preg, const char *restrict pattern,
18           int cflags);
19       size_t regerror(int errcode, const regex_t *restrict preg,
20           char *restrict errbuf, size_t errbuf_size);
21       int regexec(const regex_t *restrict preg, const char *restrict string,
22           size_t nmatch, regmatch_t pmatch[restrict], int eflags);
23       void regfree(regex_t *preg);
24

DESCRIPTION

26       These functions interpret basic and  extended  regular  expressions  as
27       described  in  the  Base Definitions volume of POSIX.1‐2017, Chapter 9,
28       Regular Expressions.
29
30       The regex_t structure is defined in <regex.h> and contains at least the
31       following member:
32
33             ┌──────────────┬──────────────┬───────────────────────────┐
34Member Type   Member Name  Description        
35             ├──────────────┼──────────────┼───────────────────────────┤
36size_t        re_nsub       │ Number  of  parenthesized │
37             │              │              │ subexpressions.           │
38             └──────────────┴──────────────┴───────────────────────────┘
39       The regmatch_t structure is defined in <regex.h> and contains at  least
40       the following members:
41
42             ┌──────────────┬──────────────┬───────────────────────────┐
43Member Type   Member Name  Description        
44             ├──────────────┼──────────────┼───────────────────────────┤
45regoff_t      rm_so         │ Byte offset from start of │
46             │              │              │ string to start  of  sub‐ │
47             │              │              │ string.                   │
48regoff_t      rm_eo         │ Byte offset from start of │
49             │              │              │ string of the first char‐ │
50             │              │              │ acter  after  the  end of │
51             │              │              │ substring.                │
52             └──────────────┴──────────────┴───────────────────────────┘
53       The regcomp() function shall compile the regular  expression  contained
54       in  the string pointed to by the pattern argument and place the results
55       in the structure pointed to by preg.  The cflags argument is  the  bit‐
56       wise-inclusive  OR  of  zero  or more of the following flags, which are
57       defined in the <regex.h> header:
58
59       REG_EXTENDED  Use Extended Regular Expressions.
60
61       REG_ICASE     Ignore case in match (see the Base Definitions volume  of
62                     POSIX.1‐2017, Chapter 9, Regular Expressions).
63
64       REG_NOSUB     Report only success/fail in regexec().
65
66       REG_NEWLINE   Change the handling of <newline> characters, as described
67                     in the text.
68
69       The default regular expression type for  pattern  is  a  Basic  Regular
70       Expression.  The  application  can specify Extended Regular Expressions
71       using the REG_EXTENDED cflags flag.
72
73       If the REG_NOSUB flag was not set in cflags, then regcomp()  shall  set
74       re_nsub  to  the  number  of parenthesized subexpressions (delimited by
75       "\(\)" in basic regular expressions or "()" in extended regular expres‐
76       sions) found in pattern.
77
78       The regexec() function compares the null-terminated string specified by
79       string with the compiled regular expression preg initialized by a  pre‐
80       vious  call  to regcomp().  If it finds a match, regexec() shall return
81       0; otherwise, it shall return non-zero indicating either no match or an
82       error.  The eflags argument is the bitwise-inclusive OR of zero or more
83       of the following flags, which are defined in the <regex.h> header:
84
85       REG_NOTBOL    The first character of the string pointed to by string is
86                     not  the  beginning  of the line. Therefore, the <circum‐
87                     flex> character ('^'), when taken as a special character,
88                     shall not match the beginning of string.
89
90       REG_NOTEOL    The  last character of the string pointed to by string is
91                     not the end of the  line.  Therefore,  the  <dollar-sign>
92                     ('$'), when taken as a special character, shall not match
93                     the end of string.
94
95       If nmatch is 0 or REG_NOSUB was set in  the  cflags  argument  to  reg‐
96       comp(), then regexec() shall ignore the pmatch argument. Otherwise, the
97       application shall ensure that the pmatch argument points  to  an  array
98       with at least nmatch elements, and regexec() shall fill in the elements
99       of that array with offsets of the substrings of string that  correspond
100       to  the  parenthesized subexpressions of pattern: pmatch[i].rm_so shall
101       be the byte offset of the beginning and pmatch[i].rm_eo  shall  be  one
102       greater than the byte offset of the end of substring i.  (Subexpression
103       i begins at the ith matched open parenthesis, counting from 1.) Offsets
104       in pmatch[0] identify the substring that corresponds to the entire reg‐
105       ular expression. Unused elements of pmatch up to pmatch[nmatch-1] shall
106       be filled with -1. If there are more than nmatch subexpressions in pat‐
107       tern (pattern itself counts as a subexpression), then  regexec()  shall
108       still do the match, but shall record only the first nmatch substrings.
109
110       When  matching a basic or extended regular expression, any given paren‐
111       thesized subexpression of pattern might participate  in  the  match  of
112       several  different substrings of string, or it might not match any sub‐
113       string even though the pattern as a  whole  did  match.  The  following
114       rules  shall  be used to determine which substrings to report in pmatch
115       when matching regular expressions:
116
117        1. If subexpression i in a regular expression is not contained  within
118           another  subexpression,  and  it  participated in the match several
119           times, then the byte offsets in pmatch[i] shall  delimit  the  last
120           such match.
121
122        2. If  subexpression  i is not contained within another subexpression,
123           and it did not participate in an otherwise  successful  match,  the
124           byte  offsets  in  pmatch[i]  shall be -1. A subexpression does not
125           participate in the match when:
126
127           '*' or "\{\}" appears immediately  after  the  subexpression  in  a
128           basic  regular expression, or '*', '?', or "{}" appears immediately
129           after the subexpression in an extended regular expression, and  the
130           subexpression did not match (matched 0 times)
131
132           or:
133
134                  '|' is used in an extended regular expression to select this
135                  subexpression  or  another,  and  the  other   subexpression
136                  matched.
137
138        3. If subexpression i is contained within another subexpression j, and
139           i is not contained within any other subexpression that is contained
140           within  j, and a match of subexpression j is reported in pmatch[j],
141           then  the  match  or  non-match  of  subexpression  i  reported  in
142           pmatch[i]  shall be as described in 1. and 2. above, but within the
143           substring reported in pmatch[j] rather than the whole  string.  The
144           offsets in pmatch[i] are still relative to the start of string.
145
146        4. If  subexpression  i  is contained in subexpression j, and the byte
147           offsets in pmatch[j] are -1, then the pointers in  pmatch[i]  shall
148           also be -1.
149
150        5. If  subexpression  i  matched  a zero-length string, then both byte
151           offsets in pmatch[i] shall be the byte offset of the  character  or
152           null terminator immediately following the zero-length string.
153
154       If,  when  regexec()  is  called, the locale is different from when the
155       regular expression was compiled, the result is undefined.
156
157       If REG_NEWLINE is not set in cflags, then a  <newline>  in  pattern  or
158       string  shall  be  treated  as an ordinary character. If REG_NEWLINE is
159       set, then <newline> shall be treated as an ordinary character except as
160       follows:
161
162        1. A  <newline> in string shall not be matched by a <period> outside a
163           bracket expression or by any form of a non-matching list  (see  the
164           Base Definitions volume of POSIX.1‐2017, Chapter 9, Regular Expres‐
165           sions).
166
167        2. A <circumflex> ('^') in pattern, when used  to  specify  expression
168           anchoring (see the Base Definitions volume of POSIX.1‐2017, Section
169           9.3.8, BRE  Expression  Anchoring),  shall  match  the  zero-length
170           string  immediately  after a <newline> in string, regardless of the
171           setting of REG_NOTBOL.
172
173        3. A <dollar-sign> ('$') in pattern, when used to  specify  expression
174           anchoring,  shall match the zero-length string immediately before a
175           <newline> in string, regardless of the setting of REG_NOTEOL.
176
177       The regfree() function frees any memory allocated by regcomp()  associ‐
178       ated with preg.
179
180       The  following constants are defined as the minimum set of error return
181       values, although other errors listed as  implementation  extensions  in
182       <regex.h> are possible:
183
184       REG_BADBR     Content  of  "\{\}"  invalid:  not  a  number, number too
185                     large, more than two numbers, first larger than second.
186
187       REG_BADPAT    Invalid regular expression.
188
189       REG_BADRPT    '?', '*', or '+' not preceded by  valid  regular  expres‐
190                     sion.
191
192       REG_EBRACE    "\{\}" imbalance.
193
194       REG_EBRACK    "[]" imbalance.
195
196       REG_ECOLLATE  Invalid collating element referenced.
197
198       REG_ECTYPE    Invalid character class type referenced.
199
200       REG_EESCAPE   Trailing <backslash> character in pattern.
201
202       REG_EPAREN    "\(\)" or "()" imbalance.
203
204       REG_ERANGE    Invalid endpoint in range expression.
205
206       REG_ESPACE    Out of memory.
207
208       REG_ESUBREG   Number in "\digit" invalid or in error.
209
210       REG_NOMATCH   regexec() failed to match.
211
212       If more than one error occurs in processing a function call, any one of
213       the possible constants may be returned, as the order  of  detection  is
214       unspecified.
215
216       The regerror() function provides a mapping from error codes returned by
217       regcomp() and regexec() to unspecified printable strings. It  generates
218       a  string corresponding to the value of the errcode argument, which the
219       application shall ensure is the last non-zero value  returned  by  reg‐
220       comp()  or  regexec()  with the given value of preg.  If errcode is not
221       such a value, the content of the generated string is unspecified.
222
223       If preg is a null pointer, but errcode is a value returned by a  previ‐
224       ous  call  to regexec() or regcomp(), the regerror() still generates an
225       error string corresponding to the value of errcode, but it might not be
226       as detailed under some implementations.
227
228       If the errbuf_size argument is not 0, regerror() shall place the gener‐
229       ated string into the buffer of size errbuf_size  bytes  pointed  to  by
230       errbuf.   If  the string (including the terminating null) cannot fit in
231       the buffer, regerror() shall truncate the string and null-terminate the
232       result.
233
234       If  errbuf_size  is 0, regerror() shall ignore the errbuf argument, and
235       return the size of the buffer needed to hold the generated string.
236
237       If the preg argument to regexec() or regfree() is not a compiled  regu‐
238       lar  expression  returned by regcomp(), the result is undefined. A preg
239       is no longer treated as a compiled regular expression after it is given
240       to regfree().
241

RETURN VALUE

243       Upon successful completion, the regcomp() function shall return 0. Oth‐
244       erwise, it shall  return  an  integer  value  indicating  an  error  as
245       described in <regex.h>, and the content of preg is undefined. If a code
246       is returned, the interpretation shall be as given in <regex.h>.
247
248       If regcomp() detects an invalid RE, it may return REG_BADPAT, or it may
249       return one of the error codes that more precisely describes the error.
250
251       Upon successful completion, the regexec() function shall return 0. Oth‐
252       erwise, it shall return REG_NOMATCH to indicate no match.
253
254       Upon successful completion, the regerror() function  shall  return  the
255       number  of  bytes needed to hold the entire generated string, including
256       the null termination. If the return value is greater than  errbuf_size,
257       the  string  returned in the buffer pointed to by errbuf has been trun‐
258       cated.
259
260       The regfree() function shall not return a value.
261

ERRORS

263       No errors are defined.
264
265       The following sections are informative.
266

EXAMPLES

268           #include <regex.h>
269
270           /*
271            * Match string against the extended regular expression in
272            * pattern, treating errors as no match.
273            *
274            * Return 1 for match, 0 for no match.
275            */
276
277           int
278           match(const char *string, char *pattern)
279           {
280               int    status;
281               regex_t    re;
282
283               if (regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB) != 0) {
284                   return(0);      /* Report error. */
285               }
286               status = regexec(&re, string, (size_t) 0, NULL, 0);
287               regfree(&re);
288               if (status != 0) {
289                   return(0);      /* Report error. */
290               }
291               return(1);
292           }
293
294       The following demonstrates how the REG_NOTBOL flag could be  used  with
295       regexec()  to  find  all substrings in a line that match a pattern sup‐
296       plied by a user.  (For simplicity of the  example,  very  little  error
297       checking is done.)
298
299
300           (void) regcomp (&re, pattern, 0);
301           /* This call to regexec() finds the first match on the line. */
302           error = regexec (&re, &buffer[0], 1, &pm, 0);
303           while (error == 0) {  /* While matches found. */
304               /* Substring found between pm.rm_so and pm.rm_eo. */
305               /* This call to regexec() finds the next match. */
306               error = regexec (&re, buffer + pm.rm_eo, 1, &pm, REG_NOTBOL);
307           }
308

APPLICATION USAGE

310       An application could use:
311
312
313           regerror(code,preg,(char *)NULL,(size_t)0)
314
315       to  find  out how big a buffer is needed for the generated string, mal‐
316       loc() a buffer to hold the string, and then call  regerror()  again  to
317       get the string. Alternatively, it could allocate a fixed, static buffer
318       that is big enough to hold most strings, and then use malloc() to allo‐
319       cate a larger buffer if it finds that this is too small.
320
321       To  match  a  pattern as described in the Shell and Utilities volume of
322       POSIX.1‐2017,  Section  2.13,  Pattern  Matching  Notation,   use   the
323       fnmatch() function.
324

RATIONALE

326       The  regexec()  function  must  fill  in all nmatch elements of pmatch,
327       where nmatch and pmatch are supplied by the application, even  if  some
328       elements of pmatch do not correspond to subexpressions in pattern.  The
329       application developer should note that there is probably no reason  for
330       using a value of nmatch that is larger than preg->re_nsub+1.
331
332       The  REG_NEWLINE  flag  supports a use of RE matching that is needed in
333       some applications like text editors. In  such  applications,  the  user
334       supplies  an  RE asking the application to find a line that matches the
335       given expression. An anchor in such an RE anchors at the  beginning  or
336       end  of  any  line.  Such  an  application can pass a sequence of <new‐
337       line>-separated lines to regexec() as a single long string and  specify
338       REG_NEWLINE  to  regcomp() to get the desired behavior. The application
339       must ensure that there are no explicit <newline> characters in  pattern
340       if  it  wants  to ensure that any match occurs entirely within a single
341       line.
342
343       The REG_NEWLINE flag affects the behavior of regexec(), but  it  is  in
344       the  cflags  parameter to regcomp() to allow flexibility of implementa‐
345       tion. Some implementations will want to generate the same  compiled  RE
346       in  regcomp()  regardless  of  the  setting  of  REG_NEWLINE  and  have
347       regexec() handle anchors differently based on the setting of the  flag.
348       Other implementations will generate different compiled REs based on the
349       REG_NEWLINE.
350
351       The REG_ICASE flag supports the operations taken by the grep -i  option
352       and  the  historical implementations of ex and vi.  Including this flag
353       will make it easier for application code to be written  that  does  the
354       same thing as these utilities.
355
356       The  substrings reported in pmatch[] are defined using offsets from the
357       start of the string rather than pointers. This allows type-safe  access
358       to both constant and non-constant strings.
359
360       The  type  regoff_t is used for the elements of pmatch[] to ensure that
361       the application can represent large arrays in memory (important for  an
362       application   conforming   to   the   Shell  and  Utilities  volume  of
363       POSIX.1‐2017).
364
365       The 1992 edition of this standard required regoff_t to be at  least  as
366       wide  as  off_t, to facilitate future extensions in which the string to
367       be searched is taken from a file. However, these future extensions have
368       not  appeared.   The requirement rules out popular implementations with
369       32-bit regoff_t and 64-bit off_t, so it has been removed.
370
371       The standard developers rejected the inclusion of a  regsub()  function
372       that  would  be used to do substitutions for a matched RE. While such a
373       routine would be useful to some applications, its utility would be much
374       more limited than the matching function described here. Both RE parsing
375       and substitution are possible to implement without support  other  than
376       that  required by the ISO C standard, but matching is much more complex
377       than substituting. The only difficult part of substitution,  given  the
378       information  supplied  by regexec(), is finding the next character in a
379       string when there can be multi-byte characters. That is a  much  larger
380       issue, and one that needs a more general solution.
381
382       The errno variable has not been used for error returns to avoid filling
383       the errno name space for this feature.
384
385       The interface is defined so that the matched substrings rm_sp and rm_ep
386       are  in  a  separate  regmatch_t structure instead of in regex_t.  This
387       allows a single compiled RE to be used simultaneously in  several  con‐
388       texts;  in main() and a signal handler, perhaps, or in multiple threads
389       of lightweight processes. (The preg argument to regexec()  is  declared
390       with  type  const,  so  the  implementation is not permitted to use the
391       structure to store intermediate results.) It also allows an application
392       to  request an arbitrary number of substrings from an RE. The number of
393       subexpressions in the RE is reported in re_nsub  in  preg.   With  this
394       change  to regexec(), consideration was given to dropping the REG_NOSUB
395       flag since the user can now specify this with a zero nmatch argument to
396       regexec().   However, keeping REG_NOSUB allows an implementation to use
397       a different (perhaps more efficient) algorithm if it knows in regcomp()
398       that  no  subexpressions  need  be reported. The implementation is only
399       required to fill in pmatch if nmatch is not zero and  if  REG_NOSUB  is
400       not specified. Note that the size_t type, as defined in the ISO C stan‐
401       dard, is unsigned, so the description of regexec()  does  not  need  to
402       address negative values of nmatch.
403
404       REG_NOTBOL  was  added  to allow an application to do repeated searches
405       for the same pattern in a line. If the pattern contains a  <circumflex>
406       character  that  should match the beginning of a line, then the pattern
407       should only match when matched  against  the  beginning  of  the  line.
408       Without  the REG_NOTBOL flag, the application could rewrite the expres‐
409       sion for subsequent matches, but in the general case this would require
410       parsing the expression. The need for REG_NOTEOL is not as clear; it was
411       added for symmetry.
412
413       The addition of the regerror() function addresses the  historical  need
414       for conforming application programs to have access to error information
415       more than ``Function failed to compile/match your RE for  unknown  rea‐
416       sons''.
417
418       This interface provides for two different methods of dealing with error
419       conditions. The specific error codes (REG_EBRACE, for example), defined
420       in <regex.h>, allow an application to recover from an error if it is so
421       able. Many applications, especially those that use patterns supplied by
422       a  user,  will not try to deal with specific error cases, but will just
423       use regerror() to obtain a human-readable error message to  present  to
424       the user.
425
426       The regerror() function uses a scheme similar to confstr() to deal with
427       the problem of allocating memory to  hold  the  generated  string.  The
428       scheme  used  by  strerror() in the ISO C standard was considered unac‐
429       ceptable since it creates difficulties for multi-threaded applications.
430
431       The preg argument is provided to regerror() to allow an  implementation
432       to  generate  a  more  descriptive  message than would be possible with
433       errcode alone. An implementation might, for example, save the character
434       offset  of  the  offending character of the pattern in a field of preg,
435       and then include that in the generated message string. The  implementa‐
436       tion may also ignore preg.
437
438       A  REG_FILENAME  flag  was  considered,  but  omitted. This flag caused
439       regexec() to match patterns as described in  the  Shell  and  Utilities
440       volume of POSIX.1‐2017, Section 2.13, Pattern Matching Notation instead
441       of REs. This service is now provided by the fnmatch() function.
442
443       Notice  that  there  is  a  difference  in   philosophy   between   the
444       ISO POSIX‐2:1993  standard  and POSIX.1‐2008 in how to handle a ``bad''
445       regular expression. The ISO POSIX‐2:1993 standard says  that  many  bad
446       constructs  ``produce undefined results'', or that ``the interpretation
447       is undefined''. POSIX.1‐2008, however, says that the interpretation  of
448       such  REs  is unspecified. The term ``undefined'' means that the action
449       by the application is an error, of similar severity to  passing  a  bad
450       pointer to a function.
451
452       The  regcomp() and regexec() functions are required to accept any null-
453       terminated string as the pattern argument. If the meaning of the string
454       is  ``undefined'',  the  behavior  of  the function is ``unspecified''.
455       POSIX.1‐2008 does not specify how the functions will interpret the pat‐
456       tern;  they might return error codes, or they might do pattern matching
457       in some completely unexpected way, but they  should  not  do  something
458       like abort the process.
459

FUTURE DIRECTIONS

461       None.
462

SEE ALSO

464       fnmatch(), glob()
465
466       The Base Definitions volume of POSIX.1‐2017, Chapter 9, Regular Expres‐
467       sions, <regex.h>, <sys_types.h>
468
469       The Shell and Utilities volume of POSIX.1‐2017, Section  2.13,  Pattern
470       Matching Notation
471
473       Portions  of  this text are reprinted and reproduced in electronic form
474       from IEEE Std 1003.1-2017, Standard for Information Technology --  Por‐
475       table  Operating System Interface (POSIX), The Open Group Base Specifi‐
476       cations Issue 7, 2018 Edition, Copyright (C) 2018 by the  Institute  of
477       Electrical  and  Electronics Engineers, Inc and The Open Group.  In the
478       event of any discrepancy between this version and the original IEEE and
479       The  Open Group Standard, the original IEEE and The Open Group Standard
480       is the referee document. The original Standard can be  obtained  online
481       at http://www.opengroup.org/unix/online.html .
482
483       Any  typographical  or  formatting  errors that appear in this page are
484       most likely to have been introduced during the conversion of the source
485       files  to  man page format. To report such errors, see https://www.ker
486       nel.org/doc/man-pages/reporting_bugs.html .
487
488
489
490IEEE/The Open Group                  2017                          REGCOMP(3P)
Impressum