3065 some functions in the tcp module can be static
[unleashed.git] / usr / src / cmd / man / src / util / instant.src / tptregexp / regexp.3
blobd714b629e44c079f24bc3f7783f80b137acf5d5f
2 #pragma ident   "%Z%%M% %I%     %E% SMI"
4 .TH REGEXP 3 local
5 .DA 30 Nov 1985
6 .SH NAME
7 regcomp, regexec, regsub, regerror \- regular expression handler
8 .SH SYNOPSIS
9 .ft B
10 .nf
11 #include <regexp.h>
13 regexp *regcomp(exp)
14 char *exp;
16 int regexec(prog, string)
17 regexp *prog;
18 char *string;
20 regsub(prog, source, dest)
21 regexp *prog;
22 char *source;
23 char *dest;
25 regerror(msg)
26 char *msg;
27 .SH DESCRIPTION
28 These functions implement
29 .IR egrep (1)-style
30 regular expressions and supporting facilities.
31 .PP
32 .I Regcomp
33 compiles a regular expression into a structure of type
34 .IR regexp ,
35 and returns a pointer to it.
36 The space has been allocated using
37 .IR malloc (3)
38 and may be released by
39 .IR free .
40 .PP
41 .I Regexec
42 matches a NUL-terminated \fIstring\fR against the compiled regular expression
43 in \fIprog\fR.
44 It returns 1 for success and 0 for failure, and adjusts the contents of
45 \fIprog\fR's \fIstartp\fR and \fIendp\fR (see below) accordingly.
46 .PP
47 The members of a
48 .I regexp
49 structure include at least the following (not necessarily in order):
50 .PP
51 .RS
52 char *startp[NSUBEXP];
53 .br
54 char *endp[NSUBEXP];
55 .RE
56 .PP
57 where
58 .I NSUBEXP
59 is defined (as 10) in the header file.
60 Once a successful \fIregexec\fR has been done using the \fIregexp\fR,
61 each \fIstartp\fR-\fIendp\fR pair describes one substring
62 within the \fIstring\fR,
63 with the \fIstartp\fR pointing to the first character of the substring and
64 the \fIendp\fR pointing to the first character following the substring.
65 The 0th substring is the substring of \fIstring\fR that matched the whole
66 regular expression.
67 The others are those substrings that matched parenthesized expressions
68 within the regular expression, with parenthesized expressions numbered
69 in left-to-right order of their opening parentheses.
70 .PP
71 .I Regsub
72 copies \fIsource\fR to \fIdest\fR, making substitutions according to the
73 most recent \fIregexec\fR performed using \fIprog\fR.
74 Each instance of `&' in \fIsource\fR is replaced by the substring
75 indicated by \fIstartp\fR[\fI0\fR] and
76 \fIendp\fR[\fI0\fR].
77 Each instance of `\e\fIn\fR', where \fIn\fR is a digit, is replaced by
78 the substring indicated by
79 \fIstartp\fR[\fIn\fR] and
80 \fIendp\fR[\fIn\fR].
81 To get a literal `&' or `\e\fIn\fR' into \fIdest\fR, prefix it with `\e';
82 to get a literal `\e' preceding `&' or `\e\fIn\fR', prefix it with
83 another `\e'.
84 .PP
85 .I Regerror
86 is called whenever an error is detected in \fIregcomp\fR, \fIregexec\fR,
87 or \fIregsub\fR.
88 The default \fIregerror\fR writes the string \fImsg\fR,
89 with a suitable indicator of origin,
90 on the standard
91 error output
92 and invokes \fIexit\fR(2).
93 .I Regerror
94 can be replaced by the user if other actions are desirable.
95 .SH "REGULAR EXPRESSION SYNTAX"
96 A regular expression is zero or more \fIbranches\fR, separated by `|'.
97 It matches anything that matches one of the branches.
98 .PP
99 A branch is zero or more \fIpieces\fR, concatenated.
100 It matches a match for the first, followed by a match for the second, etc.
102 A piece is an \fIatom\fR possibly followed by `*', `+', or `?'.
103 An atom followed by `*' matches a sequence of 0 or more matches of the atom.
104 An atom followed by `+' matches a sequence of 1 or more matches of the atom.
105 An atom followed by `?' matches a match of the atom, or the null string.
107 An atom is a regular expression in parentheses (matching a match for the
108 regular expression), a \fIrange\fR (see below), `.'
109 (matching any single character), `^' (matching the null string at the
110 beginning of the input string), `$' (matching the null string at the
111 end of the input string), a `\e' followed by a single character (matching
112 that character), or a single character with no other significance
113 (matching that character).
115 A \fIrange\fR is a sequence of characters enclosed in `[]'.
116 It normally matches any single character from the sequence.
117 If the sequence begins with `^',
118 it matches any single character \fInot\fR from the rest of the sequence.
119 If two characters in the sequence are separated by `\-', this is shorthand
120 for the full list of ASCII characters between them
121 (e.g. `[0-9]' matches any decimal digit).
122 To include a literal `]' in the sequence, make it the first character
123 (following a possible `^').
124 To include a literal `\-', make it the first or last character.
125 .SH AMBIGUITY
126 If a regular expression could match two different parts of the input string,
127 it will match the one which begins earliest.
128 If both begin in the same place but match different lengths, or match
129 the same length in different ways, life gets messier, as follows.
131 In general, the possibilities in a list of branches are considered in
132 left-to-right order, the possibilities for `*', `+', and `?' are
133 considered longest-first, nested constructs are considered from the
134 outermost in, and concatenated constructs are considered leftmost-first.
135 The match that will be chosen is the one that uses the earliest
136 possibility in the first choice that has to be made.
137 If there is more than one choice, the next will be made in the same manner
138 (earliest possibility) subject to the decision on the first choice.
139 And so forth.
141 For example, `(ab|a)b*c' could match `abc' in one of two ways.
142 The first choice is between `ab' and `a'; since `ab' is earlier, and does
143 lead to a successful overall match, it is chosen.
144 Since the `b' is already spoken for,
145 the `b*' must match its last possibility\(emthe empty string\(emsince
146 it must respect the earlier choice.
148 In the particular case where no `|'s are present and there is only one
149 `*', `+', or `?', the net effect is that the longest possible
150 match will be chosen.
151 So `ab*', presented with `xabbbby', will match `abbbb'.
152 Note that if `ab*' is tried against `xabyabbbz', it
153 will match `ab' just after `x', due to the begins-earliest rule.
154 (In effect, the decision on where to start the match is the first choice
155 to be made, hence subsequent choices must respect it even if this leads them
156 to less-preferred alternatives.)
157 .SH SEE ALSO
158 egrep(1), expr(1)
159 .SH DIAGNOSTICS
160 \fIRegcomp\fR returns NULL for a failure
161 (\fIregerror\fR permitting),
162 where failures are syntax errors, exceeding implementation limits,
163 or applying `+' or `*' to a possibly-null operand.
164 .SH HISTORY
165 Both code and manual page were
166 written at U of T.
167 They are intended to be compatible with the Bell V8 \fIregexp\fR(3),
168 but are not derived from Bell code.
169 .SH BUGS
170 Empty branches and empty regular expressions are not portable to V8.
172 The restriction against
173 applying `*' or `+' to a possibly-null operand is an artifact of the
174 simplistic implementation.
176 Does not support \fIegrep\fR's newline-separated branches;
177 neither does the V8 \fIregexp\fR(3), though.
179 Due to emphasis on
180 compactness and simplicity,
181 it's not strikingly fast.
182 It does give special attention to handling simple cases quickly.