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 one 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, e.g., the ending point of the
167 range occurs prior to the starting point.
168
169 REG_ESIZE
170 Compiled regular expression requires a pattern buffer larger
171 than 64Kb. 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 POSIX.1-2001.
181
183 grep(1), regex(7)
184 The glibc manual section, Regular Expression Matching
185
187 This page is part of release 3.53 of the Linux man-pages project. A
188 description of the project, and information about reporting bugs, can
189 be found at http://www.kernel.org/doc/man-pages/.
190
191
192
193GNU 2013-02-11 REGEX(3)