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 Byte offsets
84 Unless REG_NOSUB was set for the compilation of the pattern buffer, it
85 is possible to obtain match addressing information. pmatch must be
86 dimensioned to have at least nmatch elements. These are filled in by
87 regexec() with substring match addresses. The offsets of the subex‐
88 pression starting at the ith open parenthesis are stored in pmatch[i].
89 The entire regular expression's match addresses are stored in
90 pmatch[0]. (Note that to return the offsets of N subexpression
91 matches, nmatch must be at least N+1.) Any unused structure elements
92 will contain the value -1.
93
94 The regmatch_t structure which is the type of pmatch is defined in
95 <regex.h>.
96
97 typedef struct {
98 regoff_t rm_so;
99 regoff_t rm_eo;
100 } regmatch_t;
101
102 Each rm_so element that is not -1 indicates the start offset of the
103 next largest substring match within the string. The relative rm_eo
104 element indicates the end offset of the match, which is the offset of
105 the first character after the matching text.
106
107 POSIX error reporting
108 regerror() is used to turn the error codes that can be returned by both
109 regcomp() and regexec() into error message strings.
110
111 regerror() is passed the error code, errcode, the pattern buffer, preg,
112 a pointer to a character string buffer, errbuf, and the size of the
113 string buffer, errbuf_size. It returns the size of the errbuf required
114 to contain the null-terminated error message string. If both errbuf
115 and errbuf_size are nonzero, errbuf is filled in with the first
116 errbuf_size - 1 characters of the error message and a terminating null
117 byte ('\0').
118
119 POSIX pattern buffer freeing
120 Supplying regfree() with a precompiled pattern buffer, preg will free
121 the memory allocated to the pattern buffer by the compiling process,
122 regcomp().
123
125 regcomp() returns zero for a successful compilation or an error code
126 for failure.
127
128 regexec() returns zero for a successful match or REG_NOMATCH for fail‐
129 ure.
130
132 The following errors can be returned by regcomp():
133
134 REG_BADBR
135 Invalid use of back reference operator.
136
137 REG_BADPAT
138 Invalid use of pattern operators such as group or list.
139
140 REG_BADRPT
141 Invalid use of repetition operators such as using '*' as the
142 first character.
143
144 REG_EBRACE
145 Un-matched brace interval operators.
146
147 REG_EBRACK
148 Un-matched bracket list operators.
149
150 REG_ECOLLATE
151 Invalid collating element.
152
153 REG_ECTYPE
154 Unknown character class name.
155
156 REG_EEND
157 Nonspecific error. This is not defined by POSIX.2.
158
159 REG_EESCAPE
160 Trailing backslash.
161
162 REG_EPAREN
163 Un-matched parenthesis group operators.
164
165 REG_ERANGE
166 Invalid use of the range operator; for example, the ending point
167 of the range occurs prior to the starting point.
168
169 REG_ESIZE
170 Compiled regular expression requires a pattern buffer larger
171 than 64 kB. This is not defined by POSIX.2.
172
173 REG_ESPACE
174 The regex routines ran out of memory.
175
176 REG_ESUBREG
177 Invalid back reference to a subexpression.
178
180 For an explanation of the terms used in this section, see
181 attributes(7).
182
183 ┌─────────────────────┬───────────────┬────────────────┐
184 │Interface │ Attribute │ Value │
185 ├─────────────────────┼───────────────┼────────────────┤
186 │regcomp(), regexec() │ Thread safety │ MT-Safe locale │
187 ├─────────────────────┼───────────────┼────────────────┤
188 │regerror() │ Thread safety │ MT-Safe env │
189 ├─────────────────────┼───────────────┼────────────────┤
190 │regfree() │ Thread safety │ MT-Safe │
191 └─────────────────────┴───────────────┴────────────────┘
193 POSIX.1-2001, POSIX.1-2008.
194
196 grep(1), regex(7)
197
198 The glibc manual section, Regular Expressions
199
201 This page is part of release 5.02 of the Linux man-pages project. A
202 description of the project, information about reporting bugs, and the
203 latest version of this page, can be found at
204 https://www.kernel.org/doc/man-pages/.
205
206
207
208GNU 2019-03-06 REGEX(3)