1regcmp(3C) Standard C Library Functions regcmp(3C)
2
3
4
6 regcmp, regex - compile and execute regular expression
7
9 #include <libgen.h>
10
11 char *regcmp(const char *string1, /* char *string2 */ ...,
12 int /*(char*)0*/);
13
14
15 char *regex(const char *re, const char *subject,
16 /* char *ret0 */ ...);
17
18
19 extern char *__loc1;
20
21
23 The regcmp() function compiles a regular expression (consisting of the
24 concatenated arguments) and returns a pointer to the compiled form.
25 The malloc(3C) function is used to create space for the compiled form.
26 It is the user's responsibility to free unneeded space so allocated. A
27 NULL return from regcmp() indicates an incorrect argument. regcmp(1)
28 has been written to generally preclude the need for this routine at
29 execution time.
30
31
32 The regex() function executes a compiled pattern against the subject
33 string. Additional arguments are passed to receive values back. The
34 regex() function returns NULL on failure or a pointer to the next
35 unmatched character on success. A global character pointer __loc1
36 points to where the match began. The regcmp() and regex() functions
37 were mostly borrowed from the editor ed(1); however, the syntax and
38 semantics have been changed slightly. The following are the valid sym‐
39 bols and associated meanings.
40
41 []*.^ This group of symbols retains its meaning as
42 described on the regexp(5) manual page.
43
44
45 $ Matches the end of the string; \n matches a newline.
46
47
48 − Within brackets the minus means through. For example,
49 [a−z] is equivalent to [abcd...xyz]. The − can appear
50 as itself only if used as the first or last charac‐
51 ter. For example, the character class expression []−]
52 matches the characters ] and −.
53
54
55 + A regular expression followed by + means one or more
56 times. For example, [0−9]+ is equivalent to
57 [0−9][0−9]*.
58
59
60 {m} {m,} {m,u} Integer values enclosed in {} indicate the number of
61 times the preceding regular expression is to be
62 applied. The value m is the minimum number and u is a
63 number, less than 256, which is the maximum. If only
64 m is present (that is, {m}), it indicates the exact
65 number of times the regular expression is to be
66 applied. The value {m,} is analogous to {m,infinity}.
67 The plus (+) and star (*) operations are equivalent
68 to {1,} and {0,} respectively.
69
70
71 ( ... )$n The value of the enclosed regular expression is to be
72 returned. The value will be stored in the (n+1)th
73 argument following the subject argument. At most, ten
74 enclosed regular expressions are allowed. The regex()
75 function makes its assignments unconditionally.
76
77
78 ( ... ) Parentheses are used for grouping. An operator, for
79 example, *, +, {}, can work on a single character or
80 a regular expression enclosed in parentheses. For
81 example, (a*(cb+)*)$0. By necessity, all the above
82 defined symbols are special. They must, therefore, be
83 escaped with a \ (backslash) to be used as them‐
84 selves.
85
86
88 Example 1 Example matching a leading newline in the subject string.
89
90
91 The following example matches a leading newline in the subject string
92 pointed at by cursor.
93
94
95 char *cursor, *newcursor, *ptr;
96 ...
97 newcursor = regex((ptr = regcmp("^\n", (char *)0)), cursor);
98 free(ptr);
99
100
101
102 The following example matches through the string Testing3 and returns
103 the address of the character after the last matched character (the
104 ``4''). The string Testing3 is copied to the character array ret0.
105
106
107 char ret0[9];
108 char *newcursor, *name;
109 ...
110 name = regcmp("([A−Za−z][A−za−z0−9]{0,7})$0", (char *)0);
111 newcursor = regex(name, "012Testing345", ret0);
112
113
114
115 The following example applies a precompiled regular expression in
116 file.i (see regcmp(1)) against string.
117
118
119 #include "file.i"
120 char *string, *newcursor;
121 ...
122 newcursor = regex(name, string);
123
124
126 See attributes(5) for descriptions of the following attributes:
127
128
129
130
131 ┌─────────────────────────────┬─────────────────────────────┐
132 │ ATTRIBUTE TYPE │ ATTRIBUTE VALUE │
133 ├─────────────────────────────┼─────────────────────────────┤
134 │MT-Level │MT-Safe │
135 └─────────────────────────────┴─────────────────────────────┘
136
138 ed(1), regcmp(1), malloc(3C), attributes(5), regexp(5)
139
141 The user program may run out of memory if regcmp() is called itera‐
142 tively without freeing the vectors no longer required.
143
144
145 When compiling multithreaded applications, the _REENTRANT flag must be
146 defined on the compile line. This flag should only be used in multi‐
147 threaded applications.
148
149
150
151SunOS 5.11 14 Nov 2002 regcmp(3C)