1REGEX(3) Linux Programmer's Manual REGEX(3)
2
3
4
6 regcomp, regexec, regerror, regfree - POSIX regex functions
7
9 #include <sys/types.h>
10 #include <regex.h>
11
12 int regcomp(regex_t *preg, const char *regex, int cflags);
13
14 int regexec(const regex_t *preg, const char *string, size_t nmatch,
15 regmatch_t pmatch[], int eflags);
16
17 size_t regerror(int errcode, const regex_t *preg, char *errbuf,
18 size_t errbuf_size);
19
20 void regfree(regex_t *preg);
21
23 POSIX regex compiling
24 regcomp() is used to compile a regular expression into a form that is
25 suitable for subsequent regexec() searches.
26
27 regcomp() is supplied with preg, a pointer to a pattern buffer storage
28 area; regex, a pointer to the null-terminated string and cflags, flags
29 used to determine the type of compilation.
30
31 All regular expression searching must be done via a compiled pattern
32 buffer, thus regexec() must always be supplied with the address of a
33 regcomp() initialized pattern buffer.
34
35 cflags may be the bitwise-or of zero or more of the following:
36
37 REG_EXTENDED
38 Use POSIX Extended Regular Expression syntax when interpreting
39 regex. If not set, POSIX Basic Regular Expression syntax is
40 used.
41
42 REG_ICASE
43 Do not differentiate case. Subsequent regexec() searches using
44 this pattern buffer will be case insensitive.
45
46 REG_NOSUB
47 Do not report position of matches. The nmatch and pmatch argu‐
48 ments to regexec() are ignored if the pattern buffer supplied
49 was compiled with this flag set.
50
51 REG_NEWLINE
52 Match-any-character operators don't match a newline.
53
54 A nonmatching list ([^...]) not containing a newline does not
55 match a newline.
56
57 Match-beginning-of-line operator (^) matches the empty string
58 immediately after a newline, regardless of whether eflags, the
59 execution flags of regexec(), contains REG_NOTBOL.
60
61 Match-end-of-line operator ($) matches the empty string immedi‐
62 ately before a newline, regardless of whether eflags contains
63 REG_NOTEOL.
64
65 POSIX regex matching
66 regexec() is used to match a null-terminated string against the precom‐
67 piled pattern buffer, preg. nmatch and pmatch are used to provide
68 information regarding the location of any matches. eflags may be the
69 bitwise-or of one or both of REG_NOTBOL and REG_NOTEOL which cause
70 changes in matching behavior described below.
71
72 REG_NOTBOL
73 The match-beginning-of-line operator always fails to match (but
74 see the compilation flag REG_NEWLINE above). This flag may be
75 used when different portions of a string are passed to regexec()
76 and the beginning of the string should not be interpreted as the
77 beginning of the line.
78
79 REG_NOTEOL
80 The match-end-of-line operator always fails to match (but see
81 the compilation flag REG_NEWLINE above).
82
83 REG_STARTEND
84 Use pmatch[0] on the input string, starting at byte
85 pmatch[0].rm_so and ending before byte pmatch[0].rm_eo. This
86 allows matching embedded NUL bytes and avoids a strlen(3) on
87 large strings. It does not use nmatch on input, and does not
88 change REG_NOTBOL or REG_NEWLINE processing. This flag is a BSD
89 extension, not present in POSIX.
90
91 Byte offsets
92 Unless REG_NOSUB was set for the compilation of the pattern buffer, it
93 is possible to obtain match addressing information. pmatch must be
94 dimensioned to have at least nmatch elements. These are filled in by
95 regexec() with substring match addresses. The offsets of the subex‐
96 pression starting at the ith open parenthesis are stored in pmatch[i].
97 The entire regular expression's match addresses are stored in
98 pmatch[0]. (Note that to return the offsets of N subexpression
99 matches, nmatch must be at least N+1.) Any unused structure elements
100 will contain the value -1.
101
102 The regmatch_t structure which is the type of pmatch is defined in
103 <regex.h>.
104
105 typedef struct {
106 regoff_t rm_so;
107 regoff_t rm_eo;
108 } regmatch_t;
109
110 Each rm_so element that is not -1 indicates the start offset of the
111 next largest substring match within the string. The relative rm_eo
112 element indicates the end offset of the match, which is the offset of
113 the first character after the matching text.
114
115 POSIX error reporting
116 regerror() is used to turn the error codes that can be returned by both
117 regcomp() and regexec() into error message strings.
118
119 regerror() is passed the error code, errcode, the pattern buffer, preg,
120 a pointer to a character string buffer, errbuf, and the size of the
121 string buffer, errbuf_size. It returns the size of the errbuf required
122 to contain the null-terminated error message string. If both errbuf
123 and errbuf_size are nonzero, errbuf is filled in with the first
124 errbuf_size - 1 characters of the error message and a terminating null
125 byte ('\0').
126
127 POSIX pattern buffer freeing
128 Supplying regfree() with a precompiled pattern buffer, preg will free
129 the memory allocated to the pattern buffer by the compiling process,
130 regcomp().
131
133 regcomp() returns zero for a successful compilation or an error code
134 for failure.
135
136 regexec() returns zero for a successful match or REG_NOMATCH for fail‐
137 ure.
138
140 The following errors can be returned by regcomp():
141
142 REG_BADBR
143 Invalid use of back reference operator.
144
145 REG_BADPAT
146 Invalid use of pattern operators such as group or list.
147
148 REG_BADRPT
149 Invalid use of repetition operators such as using '*' as the
150 first character.
151
152 REG_EBRACE
153 Un-matched brace interval operators.
154
155 REG_EBRACK
156 Un-matched bracket list operators.
157
158 REG_ECOLLATE
159 Invalid collating element.
160
161 REG_ECTYPE
162 Unknown character class name.
163
164 REG_EEND
165 Nonspecific error. This is not defined by POSIX.2.
166
167 REG_EESCAPE
168 Trailing backslash.
169
170 REG_EPAREN
171 Un-matched parenthesis group operators.
172
173 REG_ERANGE
174 Invalid use of the range operator; for example, the ending point
175 of the range occurs prior to the starting point.
176
177 REG_ESIZE
178 Compiled regular expression requires a pattern buffer larger
179 than 64 kB. This is not defined by POSIX.2.
180
181 REG_ESPACE
182 The regex routines ran out of memory.
183
184 REG_ESUBREG
185 Invalid back reference to a subexpression.
186
188 For an explanation of the terms used in this section, see
189 attributes(7).
190
191 ┌─────────────────────┬───────────────┬────────────────┐
192 │Interface │ Attribute │ Value │
193 ├─────────────────────┼───────────────┼────────────────┤
194 │regcomp(), regexec() │ Thread safety │ MT-Safe locale │
195 ├─────────────────────┼───────────────┼────────────────┤
196 │regerror() │ Thread safety │ MT-Safe env │
197 ├─────────────────────┼───────────────┼────────────────┤
198 │regfree() │ Thread safety │ MT-Safe │
199 └─────────────────────┴───────────────┴────────────────┘
201 POSIX.1-2001, POSIX.1-2008.
202
204 grep(1), regex(7)
205
206 The glibc manual section, Regular Expressions
207
209 This page is part of release 5.07 of the Linux man-pages project. A
210 description of the project, information about reporting bugs, and the
211 latest version of this page, can be found at
212 https://www.kernel.org/doc/man-pages/.
213
214
215
216GNU 2019-10-10 REGEX(3)