2 .\" Copyright 1989 AT&T Copyright (c) 2002, Sun Microsystems, Inc. All Rights Reserved
3 .\" 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.
4 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing. See the License for the specific language governing permissions and limitations under the License.
5 .\" 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 the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
6 .TH REGCMP 3C "Nov 14, 2002"
8 regcmp, regex \- compile and execute regular expression
14 \fBchar *\fR\fBregcmp\fR(\fBconst char *\fR\fIstring1\fR, /* \fBchar *\fR\fIstring2\fR */ ...,
15 \fBint\fR /*(\fBchar*\fR)0*/);
20 \fBchar *\fR\fBregex\fR(\fBconst char *\fR\fIre\fR, \fBconst char *\fR\fIsubject\fR,
21 /* \fBchar *\fR\fIret0\fR */ ...);
32 The \fBregcmp()\fR function compiles a regular expression (consisting of the
33 concatenated arguments) and returns a pointer to the compiled form. The
34 \fBmalloc\fR(3C) function is used to create space for the compiled form. It is
35 the user's responsibility to free unneeded space so allocated. A \fINULL\fR
36 return from \fBregcmp()\fR indicates an incorrect argument. \fBregcmp\fR(1) has
37 been written to generally preclude the need for this routine at execution time.
40 The \fBregex()\fR function executes a compiled pattern against the subject
41 string. Additional arguments are passed to receive values back. The
42 \fBregex()\fR function returns \fINULL\fR on failure or a pointer to the next
43 unmatched character on success. A global character pointer \fB__loc1\fR points
44 to where the match began. The \fBregcmp()\fR and \fBregex()\fR functions were
45 mostly borrowed from the editor \fBed\fR(1); however, the syntax and semantics
46 have been changed slightly. The following are the valid symbols and associated
51 \fB\fB[\|]\|*\|.^\fR\fR
54 This group of symbols retains its meaning as described on the \fBregexp\fR(5)
64 Matches the end of the string; \fB\en\fR matches a newline.
73 Within brackets the minus means \fIthrough\fR. For example, \fB[a\(miz]\fR is
74 equivalent to \fB[abcd\|.\|.\|.xyz]\fR. The \fB\(mi\fR can appear as itself
75 only if used as the first or last character. For example, the character class
76 expression \fB[]\(mi]\fR matches the characters \fB]\fR and \fB\(mi\fR\&.
85 A regular expression followed by \fB+\fR means \fIone or more times\fR. For
86 example, \fB[0\(mi9]+\fR is equivalent to \fB[0\(mi9][0\(mi9]*.\fR
92 \fB\fB{\fR\fIm\fR} {\fIm,\fR} {\fIm,u\fR}\fR
95 Integer values enclosed in \fB{\|}\fR indicate the number of times the
96 preceding regular expression is to be applied. The value \fIm\fR is the minimum
97 number and \fIu\fR is a number, less than 256, which is the maximum. If only
98 \fIm\fR is present (that is, \fB{\fR\fIm\fR\fB}\fR), it indicates the exact
99 number of times the regular expression is to be applied. The value
100 \fB{\fR\fIm\fR\fB,}\fR is analogous to \fB{\fR\fIm,infinity\fR\fB}\fR. The plus
101 (\fB+\fR) and star (\fB*\fR) operations are equivalent to \fB{1,}\fR and
102 \fB{0,}\fR respectively.
108 \fB\fB( ... )$\fR\fIn\fR\fR
111 The value of the enclosed regular expression is to be returned. The value will
112 be stored in the (\fIn\fR+1)th argument following the subject argument. At
113 most, ten enclosed regular expressions are allowed. The \fBregex()\fR function
114 makes its assignments unconditionally.
123 Parentheses are used for grouping. An operator, for example, \fB*\fR, \fB+\fR,
124 \fB{\|}\fR, can work on a single character or a regular expression enclosed in
125 parentheses. For example, \fB(a*(cb+)*)$0\fR. By necessity, all the above
126 defined symbols are special. They must, therefore, be escaped with a \fB\e\fR
127 (backslash) to be used as themselves.
132 \fBExample 1 \fRExample matching a leading newline in the subject string.
135 The following example matches a leading newline in the subject string pointed
141 char *cursor, *newcursor, *ptr;
143 newcursor = regex((ptr = regcmp("^\en", (char *)0)), cursor);
150 The following example matches through the string \fBTesting3\fR and returns the
151 address of the character after the last matched character (the ``\fB4\fR'').
152 The string \fBTesting3\fR is copied to the character array \fBret0\fR.
158 char *newcursor, *name;
160 name = regcmp("([A\(miZa\(miz][A\(miza\(miz0\(mi9]{0,7})$0", (char *)0);
161 newcursor = regex(name, "012Testing345", ret0);
167 The following example applies a precompiled regular expression in \fBfile.i\fR
168 (see \fBregcmp\fR(1)) against \fIstring\fR.
174 char *string, *newcursor;
176 newcursor = regex(name, string);\fR
183 See \fBattributes\fR(5) for descriptions of the following attributes:
191 ATTRIBUTE TYPE ATTRIBUTE VALUE
199 \fBed\fR(1), \fBregcmp\fR(1), \fBmalloc\fR(3C), \fBattributes\fR(5),
204 The user program may run out of memory if \fBregcmp()\fR is called iteratively
205 without freeing the vectors no longer required.