OSDN Git Service

(split) LDP: Update original to LDP v3.50.
[linuxjm/LDP_man-pages.git] / original / man3 / regex.3
1 .\" Copyright (C), 1995, Graeme W. Wilford. (Wilf.)
2 .\"
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
7 .\"
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
12 .\"
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein.  The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" %%%LICENSE_END
24 .\"
25 .\" Wed Jun 14 16:10:28 BST 1995 Wilf. (G.Wilford@ee.surrey.ac.uk)
26 .\" Tiny change in formatting - aeb, 950812
27 .\" Modified 8 May 1998 by Joseph S. Myers (jsm28@cam.ac.uk)
28 .\"
29 .\" show the synopsis section nicely
30 .de xx
31 .in \\n(INu+\\$1
32 .ti -\\$1
33 ..
34 .TH REGEX 3 2013-02-11 "GNU" "Linux Programmer's Manual"
35 .SH NAME
36 regcomp, regexec, regerror, regfree \- POSIX regex functions
37 .SH SYNOPSIS
38 .nf
39 .B #include <sys/types.h>
40 .B #include <regex.h>
41
42 .BI "int regcomp(regex_t *" preg ", const char *" regex ", int " cflags );
43
44 .BI "int regexec(const regex_t *" preg ", const char *" string \
45 ", size_t " nmatch ,
46 .BI "            regmatch_t " pmatch[] ", int " eflags );
47
48 .BI "size_t regerror(int " errcode ", const regex_t *" preg ", char *" errbuf ,
49 .BI "                size_t " errbuf_size );
50
51 .BI "void regfree(regex_t *" preg );
52 .fi
53 .SH DESCRIPTION
54 .SS POSIX regex compiling
55 .BR regcomp ()
56 is used to compile a regular expression into a form that is suitable
57 for subsequent
58 .BR regexec ()
59 searches.
60
61 .BR regcomp ()
62 is supplied with
63 .IR preg ,
64 a pointer to a pattern buffer storage area;
65 .IR regex ,
66 a pointer to the null-terminated string and
67 .IR cflags ,
68 flags used to determine the type of compilation.
69
70 All regular expression searching must be done via a compiled pattern
71 buffer, thus
72 .BR regexec ()
73 must always be supplied with the address of a
74 .BR regcomp ()
75 initialized pattern buffer.
76
77 .I cflags
78 may be the
79 .RB bitwise- or
80 of one or more of the following:
81 .TP
82 .B REG_EXTENDED
83 Use
84 .B POSIX
85 Extended Regular Expression syntax when interpreting
86 .IR regex .
87 If not set,
88 .B POSIX
89 Basic Regular Expression syntax is used.
90 .TP
91 .B REG_ICASE
92 Do not differentiate case.
93 Subsequent
94 .BR regexec ()
95 searches using this pattern buffer will be case insensitive.
96 .TP
97 .B REG_NOSUB
98 Do not report position of matches.
99 The
100 .I nmatch
101 and
102 .I pmatch
103 arguments to
104 .BR regexec ()
105 are ignored if the pattern buffer supplied was compiled with this flag set.
106 .TP
107 .B REG_NEWLINE
108 Match-any-character operators don't match a newline.
109
110 A nonmatching list
111 .RB ( [^...] )
112 not containing a newline does not match a newline.
113
114 Match-beginning-of-line operator
115 .RB ( ^ )
116 matches the empty string immediately after a newline, regardless of
117 whether
118 .IR eflags ,
119 the execution flags of
120 .BR regexec (),
121 contains
122 .BR REG_NOTBOL .
123
124 Match-end-of-line operator
125 .RB ( $ )
126 matches the empty string immediately before a newline, regardless of
127 whether
128 .I eflags
129 contains
130 .BR REG_NOTEOL .
131 .SS POSIX regex matching
132 .BR regexec ()
133 is used to match a null-terminated string
134 against the precompiled pattern buffer,
135 .IR preg .
136 .I nmatch
137 and
138 .I pmatch
139 are used to provide information regarding the location of any matches.
140 .I eflags
141 may be the
142 .RB bitwise- or
143 of one or both of
144 .B REG_NOTBOL
145 and
146 .B REG_NOTEOL
147 which cause changes in matching behavior described below.
148 .TP
149 .B REG_NOTBOL
150 The match-beginning-of-line operator always fails to match (but see the
151 compilation flag
152 .B REG_NEWLINE
153 above)
154 This flag may be used when different portions of a string are passed to
155 .BR regexec ()
156 and the beginning of the string should not be interpreted as the
157 beginning of the line.
158 .TP
159 .B REG_NOTEOL
160 The match-end-of-line operator always fails to match (but see the
161 compilation flag
162 .B REG_NEWLINE
163 above)
164 .SS Byte offsets
165 Unless
166 .B REG_NOSUB
167 was set for the compilation of the pattern buffer, it is possible to
168 obtain match addressing information.
169 .I pmatch
170 must be dimensioned to have at least
171 .I nmatch
172 elements.
173 These are filled in by
174 .BR regexec ()
175 with substring match addresses.
176 The offsets of the subexpression starting at the
177 .IR i th
178 open parenthesis are stored in
179 .IR pmatch[i] .
180 The entire regular expression's match addresses are stored in
181 .IR pmatch[0] .
182 (Note that to return the offsets of
183 .I N
184 subexpression matches,
185 .I nmatch
186 must be at least
187 .IR N+1 .)
188 Any unused structure elements will contain the value \-1.
189
190 The
191 .I regmatch_t
192 structure which is the type of
193 .I pmatch
194 is defined in
195 .IR <regex.h> .
196
197 .in +4n
198 .nf
199 typedef struct {
200     regoff_t rm_so;
201     regoff_t rm_eo;
202 } regmatch_t;
203 .fi
204 .in
205
206 Each
207 .I rm_so
208 element that is not \-1 indicates the start offset of the next largest
209 substring match within the string.
210 The relative
211 .I rm_eo
212 element indicates the end offset of the match,
213 which is the offset of the first character after the matching text.
214 .SS POSIX error reporting
215 .BR regerror ()
216 is used to turn the error codes that can be returned by both
217 .BR regcomp ()
218 and
219 .BR regexec ()
220 into error message strings.
221
222 .BR regerror ()
223 is passed the error code,
224 .IR errcode ,
225 the pattern buffer,
226 .IR preg ,
227 a pointer to a character string buffer,
228 .IR errbuf ,
229 and the size of the string buffer,
230 .IR errbuf_size .
231 It returns the size of the
232 .I errbuf
233 required to contain the null-terminated error message string.
234 If both
235 .I errbuf
236 and
237 .I errbuf_size
238 are nonzero,
239 .I errbuf
240 is filled in with the first
241 .I "errbuf_size \- 1"
242 characters of the error message and a terminating null byte (\(aq\\0\(aq).
243 .SS POSIX pattern buffer freeing
244 Supplying
245 .BR regfree ()
246 with a precompiled pattern buffer,
247 .I preg
248 will free the memory allocated to the pattern buffer by the compiling
249 process,
250 .BR regcomp ().
251 .SH RETURN VALUE
252 .BR regcomp ()
253 returns zero for a successful compilation or an error code for failure.
254
255 .BR regexec ()
256 returns zero for a successful match or
257 .B REG_NOMATCH
258 for failure.
259 .SH ERRORS
260 The following errors can be returned by
261 .BR regcomp ():
262 .TP
263 .B REG_BADBR
264 Invalid use of back reference operator.
265 .TP
266 .B REG_BADPAT
267 Invalid use of pattern operators such as group or list.
268 .TP
269 .B REG_BADRPT
270 Invalid use of repetition operators such as using \(aq*\(aq
271 as the first character.
272 .TP
273 .B REG_EBRACE
274 Un-matched brace interval operators.
275 .TP
276 .B REG_EBRACK
277 Un-matched bracket list operators.
278 .TP
279 .B REG_ECOLLATE
280 Invalid collating element.
281 .TP
282 .B REG_ECTYPE
283 Unknown character class name.
284 .TP
285 .B REG_EEND
286 Nonspecific error.
287 This is not defined by POSIX.2.
288 .TP
289 .B REG_EESCAPE
290 Trailing backslash.
291 .TP
292 .B REG_EPAREN
293 Un-matched parenthesis group operators.
294 .TP
295 .B REG_ERANGE
296 Invalid use of the range operator, e.g., the ending point of the range
297 occurs prior to the starting point.
298 .TP
299 .B REG_ESIZE
300 Compiled regular expression requires a pattern buffer larger than 64Kb.
301 This is not defined by POSIX.2.
302 .TP
303 .B REG_ESPACE
304 The regex routines ran out of memory.
305 .TP
306 .B REG_ESUBREG
307 Invalid back reference to a subexpression.
308 .SH CONFORMING TO
309 POSIX.1-2001.
310 .SH SEE ALSO
311 .BR grep (1),
312 .BR regex (7)
313 .br
314 The glibc manual section,
315 .I "Regular Expression Matching"