Merge branch 'master' of git://github.com/illumos/illumos-gate
[unleashed.git] / usr / src / man / man5 / regexp.5
blob7ca3c1df4acb8438e92d675d2512a0c032fd9b1d
1 '\" te
2 .\"  Copyright 1989 AT&T  Copyright (c) 2002, Sun Microsystems, Inc.  All Rights Reserved  Portions Copyright (c) 1992, X/Open Company Limited  All Rights Reserved
3 .\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for permission to reproduce portions of its copyrighted documentation. Original documentation from The Open Group can be obtained online at  http://www.opengroup.org/bookstore/.
4 .\" The Institute of Electrical and Electronics Engineers and The Open Group, have given us permission to reprint portions of their documentation. In the following statement, the phrase "this text" refers to portions of the system documentation. Portions of this text
5 .\" are reprinted and reproduced in electronic form in the Sun OS Reference Manual, from IEEE Std 1003.1, 2004 Edition, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2004 by the Institute of Electrical
6 .\" and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between these versions and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html.
7 .\"  This notice shall appear on any product containing this material.
8 .\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License").  You may not use this file except in compliance with the License. You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing.
9 .\"  See the License for the specific language governing permissions and limitations under the License. When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE.  If applicable, add the following below this CDDL HEADER, with
10 .\" the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
11 .TH REGEXP 5 "May 20, 2002"
12 .SH NAME
13 regexp, compile, step, advance \- simple regular expression compile and match
14 routines
15 .SH SYNOPSIS
16 .LP
17 .nf
18 #define INIT \fIdeclarations\fR
19 #define GETC(void) \fIgetc code\fR
20 #define PEEKC(void) \fIpeekc code\fR
21 #define UNGETC(void) \fIungetc code\fR
22 #define RETURN(\fIptr\fR) \fIreturn code\fR
23 #define ERROR(\fIval\fR) \fIerror code\fR
25 extern char *\fIloc1\fR, *\fIloc2\fR, *\fIlocs\fR;
27 #include <regexp.h>
29 \fBchar *\fR\fBcompile\fR(\fBchar *\fR\fIinstring\fR, \fBchar *\fR\fIexpbuf\fR, \fBconst char *\fR\fIendfug\fR, \fBint\fR \fIeof\fR);
30 .fi
32 .LP
33 .nf
34 \fBint\fR \fBstep\fR(\fBconst char *\fR\fIstring\fR, \fBconst char *\fR\fIexpbuf\fR);
35 .fi
37 .LP
38 .nf
39 \fBint\fR \fBadvance\fR(\fBconst char *\fR\fIstring\fR, \fBconst char *\fR\fIexpbuf\fR);
40 .fi
42 .SH DESCRIPTION
43 .sp
44 .LP
45 Regular Expressions (REs) provide a mechanism to select specific strings from a
46 set of character strings. The Simple Regular Expressions described below differ
47 from the  Internationalized Regular Expressions described on the
48 \fBregex\fR(5) manual page in the following ways:
49 .RS +4
50 .TP
51 .ie t \(bu
52 .el o
53 only Basic Regular Expressions are supported
54 .RE
55 .RS +4
56 .TP
57 .ie t \(bu
58 .el o
59 the Internationalization features\(emcharacter class, equivalence class, and
60 multi-character collation\(emare not supported.
61 .RE
62 .sp
63 .LP
64 The functions \fBstep()\fR, \fBadvance()\fR, and \fBcompile()\fR are general
65 purpose regular expression matching routines to be used in programs that
66 perform regular expression matching. These functions are defined by the
67 \fB<regexp.h>\fR header.
68 .sp
69 .LP
70 The functions \fBstep()\fR and \fBadvance()\fR do pattern matching given a
71 character string and a compiled regular expression as input.
72 .sp
73 .LP
74 The function \fBcompile()\fR takes as input a regular expression as defined
75 below and produces a compiled expression that can be used with \fBstep()\fR or
76 \fBadvance()\fR.
77 .SS "Basic Regular Expressions"
78 .sp
79 .LP
80 A regular expression specifies a set of character strings. A member of this set
81 of strings is said to be matched by the regular expression. Some characters
82 have special meaning when used in a regular expression; other characters stand
83 for themselves.
84 .sp
85 .LP
86 The following \fIone-character\fR \fIRE\fRs match a \fIsingle\fR character:
87 .sp
88 .ne 2
89 .na
90 \fB1.1\fR
91 .ad
92 .RS 7n
93 An ordinary character ( \fInot\fR one of those discussed in 1.2 below) is a
94 one-character RE that matches itself.
95 .RE
97 .sp
98 .ne 2
99 .na
100 \fB1.2\fR
102 .RS 7n
103 A backslash (\fB\|\e\fR\|) followed by any special character is a one-character
104 RE that matches the special character itself. The special characters are:
106 .ne 2
108 \fBa.\fR
110 .RS 6n
111 \fB\&.\fR, \fB*\fR, \fB[\fR\|, and \fB\e\fR (period, asterisk, left square
112 bracket, and backslash, respectively), which are always special, \fIexcept\fR
113 when they appear within square brackets (\fB[\|]\fR; see 1.4 below).
117 .ne 2
119 \fBb.\fR
121 .RS 6n
122 ^ (caret or circumflex), which is special at the \fIbeginning\fR of an
123 \fIentire\fR RE (see 4.1 and 4.3 below), or when it immediately follows the
124 left of a pair of square brackets (\fB[\|]\fR) (see 1.4 below).
128 .ne 2
130 \fBc.\fR
132 .RS 6n
133 \fB$\fR (dollar sign), which is special at the \fBend\fR of an \fIentire\fR RE
134 (see 4.2 below).
138 .ne 2
140 \fBd.\fR
142 .RS 6n
143 The character used to bound (that is, delimit) an entire RE, which is special
144 for that RE (for example, see how slash (\fB/\fR) is used in the \fBg\fR
145 command, below.)
151 .ne 2
153 \fB1.3\fR
155 .RS 7n
156 A period (\fB\&.\fR) is a one-character RE that matches any character except
157 new-line.
161 .ne 2
163 \fB1.4\fR
165 .RS 7n
166 A non-empty string of characters enclosed in square brackets (\fB[\|]\fR) is a
167 one-character RE that matches \fIany one\fR character in that string. If,
168 however, the first character of the string is a circumflex (^), the
169 one-character RE matches any character \fIexcept\fR new-line and the remaining
170 characters in the string. The ^ has this special meaning \fIonly\fR if it
171 occurs first in the string. The minus (\fB-\fR) may be used to indicate a range
172 of consecutive characters; for example, \fB[0-9]\fR is equivalent to
173 \fB[0123456789]\fR. The \fB-\fR loses this special meaning if it occurs first
174 (after an initial ^, if any) or last in the string. The right square bracket
175 (\fB]\fR) does not terminate such a string when it is the first character
176 within it (after an initial ^, if any); for example, \fB[\|]a-f]\fR matches
177 either a right square bracket (\fB]\fR) or one of the \fBASCII\fR letters
178 \fBa\fR through \fBf\fR inclusive. The four characters listed in 1.2.a above
179 stand for themselves within such a string of characters.
184 The following rules may be used to construct REs from one-character REs:
186 .ne 2
188 \fB2.1\fR
190 .RS 7n
191 A one-character RE is a RE that matches whatever the one-character RE matches.
195 .ne 2
197 \fB2.2\fR
199 .RS 7n
200 A one-character RE followed by an asterisk (\fB*\fR) is a RE that matches
201 \fB0\fR or more occurrences of the one-character RE. If there is any choice,
202 the longest leftmost string that permits a match is chosen.
206 .ne 2
208 \fB2.3\fR
210 .RS 7n
211 A one-character RE followed by \fB\e{\fR\fIm\fR\fB\e}\fR,
212 \fB\e{\fR\fIm,\fR\fB\e}\fR, or \fB\e{\fR\fIm,n\fR\fB\e}\fR is a RE that matches
213 a \fIrange\fR of occurrences of the one-character RE. The values of \fIm\fR and
214 \fIn\fR must be non-negative integers less than 256; \fB\e{\fR\fIm\fR\fB\e}\fR
215 matches \fIexactly\fR \fIm\fR occurrences; \fB\e{\fR\fIm,\fR\fB\e}\fR matches
216 \fIat least\fR \fIm\fR occurrences; \fB\e{\fR\fIm,n\fR\fB\e}\fR matches \fIany
217 number\fR of occurrences \fIbetween\fR \fIm\fR and \fIn\fR inclusive. Whenever
218 a choice exists, the RE matches as many occurrences as possible.
222 .ne 2
224 \fB2.4\fR
226 .RS 7n
227 The concatenation of REs is a RE that matches the concatenation of the strings
228 matched by each component of the RE.
232 .ne 2
234 \fB2.5\fR
236 .RS 7n
237 A RE enclosed between the character sequences \fB\e\|(\fR and \fB\e\|)\fR is a
238 RE that matches whatever the unadorned RE matches.
242 .ne 2
244 \fB2.6\fR
246 .RS 7n
247 The expression \fB\e\|\fR\fIn\fR matches the same string of characters as was
248 matched by an expression enclosed between \fB\e\|(\fR and \fB\e\|)\fR
249 \fIearlier\fR in the same RE. Here \fIn\fR is a digit; the sub-expression
250 specified is that beginning with the \fIn\fR-th occurrence of \fB\|\e\|(\fR
251 counting from the left. For example, the expression
252 ^\|\fB\e\|(\|.\|*\|\e\|)\|\e\|1\|$\fR matches a line consisting of two repeated
253 appearances of the same string.
258 An RE may be constrained to match words.
260 .ne 2
262 \fB3.1\fR
264 .RS 7n
265 \fB\e\|<\fR constrains a RE to match the beginning of a string or to follow a
266 character that is not a digit, underscore, or letter. The first character
267 matching the RE must be a digit, underscore, or letter.
271 .ne 2
273 \fB3.2\fR
275 .RS 7n
276 \fB\e\|>\fR constrains a RE to match the end of a string or to precede a
277 character that is not a digit, underscore, or letter.
282 An \fIentire\fR \fIRE\fR may be constrained to match only an initial segment or
283 final segment of a line (or both).
285 .ne 2
287 \fB4.1\fR
289 .RS 7n
290 A circumflex (^) at the beginning of an entire RE constrains that RE to match
291 an \fIinitial\fR segment of a line.
295 .ne 2
297 \fB4.2\fR
299 .RS 7n
300 A dollar sign (\fB$\fR) at the end of an entire RE constrains that RE to match
301 a \fIfinal\fR segment of a line.
305 .ne 2
307 \fB4.3\fR
309 .RS 7n
310 The construction ^\fIentire RE\fR\|\fB$\fR constrains the entire RE to match
311 the entire line.
316 The null RE (for example, \fB//\|\fR) is equivalent to the last RE encountered.
317 .SS "Addressing with REs"
320 Addresses are constructed as follows:
321 .RS +4
324 The character "\fB\&.\fR" addresses the current line.
326 .RS +4
329 The character "\fB$\fR" addresses the last line of the buffer.
331 .RS +4
334 A decimal number \fIn\fR addresses the \fIn\fR-th line of the buffer.
336 .RS +4
339 \fI\&'x\fR addresses the line marked with the mark name character \fIx\fR,
340 which must be an ASCII lower-case letter (\fBa\fR-\fBz\fR). Lines are marked
341 with the \fBk\fR command described below.
343 .RS +4
346 A RE enclosed by slashes (\fB/\fR) addresses the first line found by
347 searching \fIforward\fR from the line \fIfollowing\fR the current line toward
348 the end of the buffer and stopping at the first line containing a string
349 matching the RE. If necessary, the search wraps around to the beginning of the
350 buffer and continues up to and including the current line, so that the entire
351 buffer is searched.
353 .RS +4
356 A RE enclosed in question marks (\fB?\fR) addresses the first line found by
357 searching \fIbackward\fR from the line \fIpreceding\fR the current line toward
358 the beginning of the buffer and stopping at the first line containing a string
359 matching the RE. If necessary, the search wraps around to the end of the buffer
360 and continues up to and including the current line.
362 .RS +4
365 An address followed by a plus sign (\fB+\fR) or a minus sign (\fB-\fR)
366 followed by a decimal number specifies that address plus (respectively minus)
367 the indicated number of lines. A shorthand for .+5 is .5.
369 .RS +4
372 If an address begins with \fB+\fR or \fB-\fR, the addition or subtraction is
373 taken with respect to the current line; for example, \fB-5\fR is understood to
374 mean \fB\&.-5\fR.
376 .RS +4
379 If an address ends with \fB+\fR or \fB-\fR, then 1 is added to or subtracted
380 from the address, respectively. As a consequence of this rule and of Rule 8,
381 immediately above, the address \fB-\fR refers to the line preceding the current
382 line. (To maintain compatibility with earlier versions of the editor, the
383 character ^ in addresses is entirely equivalent to \fB-\fR\&.) Moreover,
384 trailing \fB+\fR and \fB-\fR characters have a cumulative effect, so \fB--\fR
385 refers to the current line less 2.
387 .RS +4
390 For convenience, a comma (\fB,\fR) stands for the address pair \fB1,$\fR,
391 while a semicolon (\fB;\fR) stands for the pair \fB\&.,$\fR.
393 .SS "Characters With Special Meaning"
396 Characters that have special meaning except when they appear within square
397 brackets (\fB[\|]\fR) or are preceded by \fB\e\fR are:  \fB\&.\fR, \fB*\fR,
398 \fB[\|\fR, \fB\e\fR\|. Other special characters, such as \fB$\fR have special
399 meaning in more restricted contexts.
402 The character \fB^\fR at the beginning of an expression permits a successful
403 match only immediately after a newline, and the character \fB$\fR at the end of
404 an expression requires a trailing newline.
407 Two characters have special meaning only when used within square brackets. The
408 character \fB-\fR denotes a range, \fB[\|\fR\fIc\fR\fB-\fR\fIc\fR\fB]\fR,
409 unless it is just after the open bracket or before the closing bracket,
410 \fB[\|-\fR\fIc\fR\fB]\fR or \fB[\|\fR\fIc\fR\fB-]\fR in which case it has no
411 special meaning. When used within brackets, the character \fB^\fR has the
412 meaning \fIcomplement of\fR if it immediately follows the open bracket
413 (example: \fB[^\fR\fIc\fR\fB]\|\fR); elsewhere between brackets (example:
414 \fB[\fR\fIc\fR\fB^]\|\fR) it stands for the ordinary character \fB^\fR.
417 The special meaning of the \fB\e\fR operator can be escaped only by preceding
418 it with another \fB\e\fR\|, for example \fB\e\e\fR\|.
419 .SS "Macros"
422 Programs must have the following five macros declared before the \fB#include
423 <regexp.h>\fR statement. These macros are used by the \fBcompile()\fR routine.
424 The macros \fBGETC\fR, \fBPEEKC\fR, and \fBUNGETC\fR operate on the regular
425 expression given as input to \fBcompile()\fR.
427 .ne 2
429 \fB\fBGETC\fR\fR
431 .RS 15n
432 This macro returns the value of the next character (byte) in the regular
433 expression pattern. Successive calls to  \fBGETC\fR should return successive
434 characters of the regular expression.
438 .ne 2
440 \fB\fBPEEKC\fR\fR
442 .RS 15n
443 This macro returns the next character (byte) in the regular expression.
444 Immediately successive calls to  \fBPEEKC\fR should return the same character,
445 which should also be the next character returned by \fBGETC\fR.
449 .ne 2
451 \fB\fBUNGETC\fR\fR
453 .RS 15n
454 This macro causes the argument \fBc\fR to be returned by the next call to
455 \fBGETC\fR and \fBPEEKC\fR. No more than one character of pushback is ever
456 needed and this character is guaranteed to be the last character read by
457 \fBGETC\fR. The return value of the macro \fBUNGETC(c)\fR is always ignored.
461 .ne 2
463 \fB\fBRETURN(\fR\fIptr\fR\fB)\fR\fR
465 .RS 15n
466 This macro is used on normal exit of the \fBcompile()\fR routine. The value of
467 the argument \fIptr\fR is a pointer to the character after the last character
468 of the compiled regular expression. This is useful to programs which have
469 memory allocation to manage.
473 .ne 2
475 \fB\fBERROR(\fR\fIval\fR\fB)\fR\fR
477 .RS 15n
478 This macro is the abnormal return from the \fBcompile()\fR routine. The
479 argument \fIval\fR is an error number (see \fBERRORS\fR below for meanings).
480 This call should never return.
483 .SS "\fBcompile()\fR"
486 The syntax of the \fBcompile()\fR routine is as follows:
488 .in +2
490 \fBcompile(\fR\fIinstring\fR\fB,\fR \fIexpbuf\fR\fB,\fR \fIendbuf\fR\fB,\fR \fIeof\fR\fB)\fR
492 .in -2
497 The first parameter, \fIinstring\fR, is never used explicitly by the
498 \fBcompile()\fR routine but is useful for programs that pass down different
499 pointers to input characters. It is sometimes used in the \fBINIT\fR
500 declaration (see below). Programs which call functions to input characters or
501 have characters in an external array can pass down a value of \fB(char *)0\fR
502 for this parameter.
505 The next parameter, \fIexpbuf\fR, is a character pointer. It points to the
506 place where the compiled regular expression will be placed.
509 The parameter \fIendbuf\fR is one more than the highest address where the
510 compiled regular expression may be placed. If the compiled expression cannot
511 fit in \fB(endbuf-expbuf)\fR bytes, a call to \fBERROR(50)\fR is made.
514 The parameter \fIeof\fR is the character which marks the end of the regular
515 expression. This character is usually a \fB/\fR.
518 Each program that includes the \fB<regexp.h>\fR header file must have a
519 \fB#define\fR statement for \fBINIT\fR. It is used for dependent declarations
520 and initializations. Most often it is used to set a register variable to point
521 to the beginning of the regular expression so that this register variable can
522 be used in the declarations for \fBGETC\fR, \fBPEEKC\fR, and \fBUNGETC\fR.
523 Otherwise it can be used to declare external variables that might be used by
524 \fBGETC\fR, \fBPEEKC\fR and \fBUNGETC\fR. (See \fBEXAMPLES\fR below.)
525 .SS "step(\|), advance(\|)"
528 The first parameter to the \fBstep()\fR and \fBadvance()\fR functions is a
529 pointer to a string of characters to be checked for a match. This string should
530 be null terminated.
533 The second parameter, \fIexpbuf\fR, is the compiled regular expression which
534 was obtained by a call to the function \fBcompile()\fR.
537 The function \fBstep()\fR returns non-zero if some substring of \fIstring\fR
538 matches the regular expression in \fIexpbuf\fR and  \fB0\fR if there is no
539 match. If there is a match, two external character pointers are set as a side
540 effect to the call to \fBstep()\fR. The variable \fBloc1\fR points to the first
541 character that matched the regular expression; the variable \fBloc2\fR points
542 to the character after the last character that matches the regular expression.
543 Thus if the regular expression matches the entire input string, \fBloc1\fR will
544 point to the first character of \fIstring\fR and \fBloc2\fR will point to the
545 null at the end of \fIstring\fR.
548 The function \fBadvance()\fR returns non-zero if the initial substring of
549 \fIstring\fR matches the regular expression in \fIexpbuf\fR. If there is a
550 match, an external character pointer, \fBloc2\fR, is set as a side effect. The
551 variable \fBloc2\fR points to the next character in \fIstring\fR after the last
552 character that matched.
555 When \fBadvance()\fR encounters a \fB*\fR or \fB\e{ \e}\fR sequence in the
556 regular expression, it will advance its pointer to the string to be matched as
557 far as possible and will recursively call itself trying to match the rest of
558 the string to the rest of the regular expression. As long as there is no match,
559 \fBadvance()\fR will back up along the string until it finds a match or reaches
560 the point in the string that initially matched the  \fB*\fR or \fB\e{ \e}\fR\&.
561 It is sometimes desirable to stop this backing up before the initial point in
562 the string is reached. If the external character pointer \fBlocs\fR is equal to
563 the point in the string at sometime during the backing up process,
564 \fBadvance()\fR will break out of the loop that backs up and will return zero.
567 The external variables \fBcircf\fR, \fBsed\fR, and \fBnbra\fR are reserved.
568 .SH EXAMPLES
570 \fBExample 1 \fRUsing Regular Expression Macros and Calls
573 The following is an example of how the regular expression macros and calls
574 might be defined by an application program:
577 .in +2
579 #define INIT       register char *sp = instring;
580 #define GETC()     (*sp++)
581 #define PEEKC()    (*sp)
582 #define UNGETC(c)  (--sp)
583 #define RETURN(c)  return;
584 #define ERROR(c)   regerr()
586 #include <regexp.h>
587  . . .
588       (void) compile(*argv, expbuf, &expbuf[ESIZE],'\e0');
589  . . .
590       if (step(linebuf, expbuf))
591                         succeed;
593 .in -2
596 .SH DIAGNOSTICS
599 The function \fBcompile()\fR uses the macro \fBRETURN\fR on success and the
600 macro \fBERROR\fR on failure (see above). The functions \fBstep()\fR and
601 \fBadvance()\fR return non-zero on a successful match and zero if there is no
602 match. Errors are:
604 .ne 2
606 \fB11\fR
608 .RS 6n
609 range endpoint too large.
613 .ne 2
615 \fB16\fR
617 .RS 6n
618 bad number.
622 .ne 2
624 \fB25\fR
626 .RS 6n
627 \fB\e\fR \fIdigit\fR out of range.
631 .ne 2
633 \fB36\fR
635 .RS 6n
636 illegal or missing delimiter.
640 .ne 2
642 \fB41\fR
644 .RS 6n
645 no remembered search string.
649 .ne 2
651 \fB42\fR
653 .RS 6n
654 \fB\e( \e)\fR imbalance.
658 .ne 2
660 \fB43\fR
662 .RS 6n
663 too many \fB\e(\fR\&.
667 .ne 2
669 \fB44\fR
671 .RS 6n
672 more than 2 numbers given in \fB\e{ \e}\fR\&.
676 .ne 2
678 \fB45\fR
680 .RS 6n
681 \fB}\fR expected after \fB\e\fR\&.
685 .ne 2
687 \fB46\fR
689 .RS 6n
690 first number exceeds second in \fB\e{ \e}\fR\&.
694 .ne 2
696 \fB49\fR
698 .RS 6n
699 \fB[ ]\fR imbalance.
703 .ne 2
705 \fB50\fR
707 .RS 6n
708 regular expression overflow.
711 .SH SEE ALSO
714 \fBregex\fR(5)