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 Support for substring addressing of matches is not required.
48 The nmatch and pmatch arguments to regexec() are ignored if the
49 pattern buffer supplied was compiled with this flag set.
50
51 REG_NEWLINE
52 Match-any-character operators don't match a newline.
53
54 A non-matching 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 substring match addressing information. pmatch
86 must be dimensioned to have at least nmatch elements. These are filled
87 in by regexec() with substring match addresses. Any unused structure
88 elements will contain the value -1.
89
90 The regmatch_t structure which is the type of pmatch is defined in
91 <regex.h>.
92
93 typedef struct {
94 regoff_t rm_so;
95 regoff_t rm_eo;
96 } regmatch_t;
97
98 Each rm_so element that is not -1 indicates the start offset of the
99 next largest substring match within the string. The relative rm_eo
100 element indicates the end offset of the match, which is the offset of
101 the first character after the matching text.
102
103 Posix Error Reporting
104 regerror() is used to turn the error codes that can be returned by both
105 regcomp() and regexec() into error message strings.
106
107 regerror() is passed the error code, errcode, the pattern buffer, preg,
108 a pointer to a character string buffer, errbuf, and the size of the
109 string buffer, errbuf_size. It returns the size of the errbuf required
110 to contain the null-terminated error message string. If both errbuf
111 and errbuf_size are non-zero, errbuf is filled in with the first
112 errbuf_size - 1 characters of the error message and a terminating null.
113
114 POSIX Pattern Buffer Freeing
115 Supplying regfree() with a precompiled pattern buffer, preg will free
116 the memory allocated to the pattern buffer by the compiling process,
117 regcomp().
118
120 regcomp() returns zero for a successful compilation or an error code
121 for failure.
122
123 regexec() returns zero for a successful match or REG_NOMATCH for fail‐
124 ure.
125
127 The following errors can be returned by regcomp():
128
129 REG_BADBR
130 Invalid use of back reference operator.
131
132 REG_BADPAT
133 Invalid use of pattern operators such as group or list.
134
135 REG_BADRPT
136 Invalid use of repetition operators such as using '*' as the
137 first character.
138
139 REG_EBRACE
140 Un-matched brace interval operators.
141
142 REG_EBRACK
143 Un-matched bracket list operators.
144
145 REG_ECOLLATE
146 Invalid collating element.
147
148 REG_ECTYPE
149 Unknown character class name.
150
151 REG_EEND
152 Non specific error. This is not defined by POSIX.2.
153
154 REG_EESCAPE
155 Trailing backslash.
156
157 REG_EPAREN
158 Un-matched parenthesis group operators.
159
160 REG_ERANGE
161 Invalid use of the range operator, e.g., the ending point of the
162 range occurs prior to the starting point.
163
164 REG_ESIZE
165 Compiled regular expression requires a pattern buffer larger
166 than 64Kb. This is not defined by POSIX.2.
167
168 REG_ESPACE
169 The regex routines ran out of memory.
170
171 REG_ESUBREG
172 Invalid back reference to a subexpression.
173
175 POSIX.1-2001.
176
178 grep(1), regex(7), GNU regex manual
179
181 This page is part of release 3.22 of the Linux man-pages project. A
182 description of the project, and information about reporting bugs, can
183 be found at http://www.kernel.org/doc/man-pages/.
184
185
186
187GNU 2008-05-29 REGEX(3)