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 *restrict preg, const char *restrict regex,
12                   int cflags);
13       int regexec(const regex_t *restrict preg, const char *restrict string,
14                   size_t nmatch, regmatch_t pmatch[restrict], int eflags);
15
16       size_t regerror(int errcode, const regex_t *restrict preg,
17                   char *restrict errbuf, size_t errbuf_size);
18       void regfree(regex_t *preg);
19

DESCRIPTION

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

RETURN VALUE

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

ERRORS

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

ATTRIBUTES

185       For  an  explanation  of  the  terms  used  in  this  section,  see at‐
186       tributes(7).
187
188       ┌─────────────────────────────────────┬───────────────┬────────────────┐
189Interface                            Attribute     Value          
190       ├─────────────────────────────────────┼───────────────┼────────────────┤
191regcomp(), regexec()                 │ Thread safety │ MT-Safe locale │
192       ├─────────────────────────────────────┼───────────────┼────────────────┤
193regerror()                           │ Thread safety │ MT-Safe env    │
194       ├─────────────────────────────────────┼───────────────┼────────────────┤
195regfree()                            │ Thread safety │ MT-Safe        │
196       └─────────────────────────────────────┴───────────────┴────────────────┘
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.12 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                               2021-03-22                          REGEX(3)
Impressum