1REGEX(3)                   Linux Programmer's Manual                  REGEX(3)
2
3
4

NAME

6       regcomp, regexec, regerror, regfree - POSIX regex functions
7

SYNOPSIS

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

DESCRIPTION

22   POSIX regex compiling
23       regcomp()  is  used to compile a regular expression into a form that is
24       suitable for subsequent regexec() searches.
25
26       regcomp() is supplied with preg, a pointer to a pattern buffer  storage
27       area;  regex, a pointer to the null-terminated string and cflags, flags
28       used to determine the type of compilation.
29
30       All regular expression searching must be done via  a  compiled  pattern
31       buffer,  thus  regexec()  must always be supplied with the address of a
32       regcomp() initialized pattern buffer.
33
34       cflags is the bitwise-or of zero or more of the following:
35
36       REG_EXTENDED
37              Use POSIX Extended Regular Expression syntax  when  interpreting
38              regex.   If  not  set,  POSIX Basic Regular Expression syntax is
39              used.
40
41       REG_ICASE
42              Do not differentiate case.  Subsequent regexec() searches  using
43              this pattern buffer will be case insensitive.
44
45       REG_NOSUB
46              Do  not report position of matches.  The nmatch and pmatch argu‐
47              ments to regexec() are ignored if the  pattern  buffer  supplied
48              was compiled with this flag set.
49
50       REG_NEWLINE
51              Match-any-character operators don't match a newline.
52
53              A  nonmatching  list ([^...])  not containing a newline does not
54              match a newline.
55
56              Match-beginning-of-line operator (^) matches  the  empty  string
57              immediately  after  a newline, regardless of whether eflags, the
58              execution flags of regexec(), contains REG_NOTBOL.
59
60              Match-end-of-line operator ($) matches the empty string  immedi‐
61              ately  before  a  newline, regardless of whether eflags contains
62              REG_NOTEOL.
63
64   POSIX regex matching
65       regexec() is used to match a null-terminated string against the precom‐
66       piled  pattern buffer, preg.  nmatch and pmatch are used to provide in‐
67       formation regarding the location of any matches.  eflags  is  the  bit‐
68       wise-or of zero or more of the following flags:
69
70       REG_NOTBOL
71              The  match-beginning-of-line operator always fails to match (but
72              see the compilation flag REG_NEWLINE above).  This flag  may  be
73              used when different portions of a string are passed to regexec()
74              and the beginning of the string should not be interpreted as the
75              beginning of the line.
76
77       REG_NOTEOL
78              The  match-end-of-line  operator  always fails to match (but see
79              the compilation flag REG_NEWLINE above).
80
81       REG_STARTEND
82              Use  pmatch[0]  on  the   input   string,   starting   at   byte
83              pmatch[0].rm_so  and  ending  before byte pmatch[0].rm_eo.  This
84              allows matching embedded NUL bytes and  avoids  a  strlen(3)  on
85              large  strings.   It  does not use nmatch on input, and does not
86              change REG_NOTBOL or REG_NEWLINE processing.  This flag is a BSD
87              extension, not present in POSIX.
88
89   Byte offsets
90       Unless  REG_NOSUB was set for the compilation of the pattern buffer, it
91       is possible to obtain match addressing information.  pmatch must be di‐
92       mensioned  to  have  at  least nmatch elements.  These are filled in by
93       regexec() with substring match addresses.  The offsets  of  the  subex‐
94       pression  starting at the ith open parenthesis are stored in pmatch[i].
95       The  entire  regular  expression's  match  addresses  are   stored   in
96       pmatch[0].   (Note  that  to  return  the  offsets  of  N subexpression
97       matches, nmatch must be at least N+1.)  Any unused  structure  elements
98       will contain the value -1.
99
100       The  regmatch_t  structure  which  is  the type of pmatch is defined in
101       <regex.h>.
102
103           typedef struct {
104               regoff_t rm_so;
105               regoff_t rm_eo;
106           } regmatch_t;
107
108       Each rm_so element that is not -1 indicates the  start  offset  of  the
109       next largest substring match within the string.  The relative rm_eo el‐
110       ement indicates the end offset of the match, which is the offset of the
111       first character after the matching text.
112
113   POSIX error reporting
114       regerror() is used to turn the error codes that can be returned by both
115       regcomp() and regexec() into error message strings.
116
117       regerror() is passed the error code, errcode, the pattern buffer, preg,
118       a  pointer  to  a  character string buffer, errbuf, and the size of the
119       string buffer, errbuf_size.  It returns the size of the errbuf required
120       to  contain  the  null-terminated error message string.  If both errbuf
121       and errbuf_size are nonzero, errbuf is filled in  with  the  first  er‐
122       rbuf_size  -  1  characters of the error message and a terminating null
123       byte ('\0').
124
125   POSIX pattern buffer freeing
126       Supplying regfree() with a precompiled pattern buffer, preg  will  free
127       the  memory  allocated  to the pattern buffer by the compiling process,
128       regcomp().
129

RETURN VALUE

131       regcomp() returns zero for a successful compilation or  an  error  code
132       for failure.
133
134       regexec()  returns zero for a successful match or REG_NOMATCH for fail‐
135       ure.
136

ERRORS

138       The following errors can be returned by regcomp():
139
140       REG_BADBR
141              Invalid use of back reference operator.
142
143       REG_BADPAT
144              Invalid use of pattern operators such as group or list.
145
146       REG_BADRPT
147              Invalid use of repetition operators such as  using  '*'  as  the
148              first character.
149
150       REG_EBRACE
151              Un-matched brace interval operators.
152
153       REG_EBRACK
154              Un-matched bracket list operators.
155
156       REG_ECOLLATE
157              Invalid collating element.
158
159       REG_ECTYPE
160              Unknown character class name.
161
162       REG_EEND
163              Nonspecific error.  This is not defined by POSIX.2.
164
165       REG_EESCAPE
166              Trailing backslash.
167
168       REG_EPAREN
169              Un-matched parenthesis group operators.
170
171       REG_ERANGE
172              Invalid use of the range operator; for example, the ending point
173              of the range occurs prior to the starting point.
174
175       REG_ESIZE
176              Compiled regular expression requires  a  pattern  buffer  larger
177              than 64 kB.  This is not defined by POSIX.2.
178
179       REG_ESPACE
180              The regex routines ran out of memory.
181
182       REG_ESUBREG
183              Invalid back reference to a subexpression.
184

ATTRIBUTES

186       For  an  explanation  of  the  terms  used  in  this  section,  see at‐
187       tributes(7).
188
189       ┌─────────────────────┬───────────────┬────────────────┐
190Interface            Attribute     Value          
191       ├─────────────────────┼───────────────┼────────────────┤
192regcomp(), regexec() │ Thread safety │ MT-Safe locale │
193       ├─────────────────────┼───────────────┼────────────────┤
194regerror()           │ Thread safety │ MT-Safe env    │
195       ├─────────────────────┼───────────────┼────────────────┤
196regfree()            │ Thread safety │ MT-Safe        │
197       └─────────────────────┴───────────────┴────────────────┘

CONFORMING TO

199       POSIX.1-2001, POSIX.1-2008.
200

EXAMPLES

202       #include <stdint.h>
203       #include <stdio.h>
204       #include <stdlib.h>
205       #include <regex.h>
206
207       #define ARRAY_SIZE(arr) (sizeof((arr)) / sizeof((arr)[0]))
208
209       static const char *const str =
210               "1) John Driverhacker;\n2) John Doe;\n3) John Foo;\n";
211       static const char *const re = "John.*o";
212
213       int main(void)
214       {
215           static const char *s = str;
216           regex_t     regex;
217           regmatch_t  pmatch[1];
218           regoff_t    off, len;
219
220           if (regcomp(&regex, re, REG_NEWLINE))
221               exit(EXIT_FAILURE);
222
223           printf("String = \"%s\"\n", str);
224           printf("Matches:\n");
225
226           for (int i = 0; ; i++) {
227               if (regexec(&regex, s, ARRAY_SIZE(pmatch), pmatch, 0))
228                   break;
229
230               off = pmatch[0].rm_so + (s - str);
231               len = pmatch[0].rm_eo - pmatch[0].rm_so;
232               printf("#%d:\n", i);
233               printf("offset = %jd; length = %jd\n", (intmax_t) off,
234                       (intmax_t) len);
235               printf("substring = \"%.*s\"\n", len, s + pmatch[0].rm_so);
236
237               s += pmatch[0].rm_eo;
238           }
239
240           exit(EXIT_SUCCESS);
241       }
242

SEE ALSO

244       grep(1), regex(7)
245
246       The glibc manual section, Regular Expressions
247

COLOPHON

249       This page is part of release 5.10 of the Linux  man-pages  project.   A
250       description  of  the project, information about reporting bugs, and the
251       latest    version    of    this    page,    can     be     found     at
252       https://www.kernel.org/doc/man-pages/.
253
254
255
256GNU                               2020-08-13                          REGEX(3)
Impressum