|
|
#include <libgen.h>char *regcmp (string1 [, string2, . . . (char *)0) char *string1, *string2, . . .
char *regex (re, subject, [ ret0, . . . ]) char *re, *subject, *ret0, . . .
extern char *__loc1;
The regex routine executes a compiled pattern against the subject string. Additional arguments are passed to receive values back. regex returns NULL on failure or a pointer to the next unmatched character on success. A global character pointer __loc1 points to where the match began.
The regex and regcmp routines were borrowed from the editor, ed(C); however, the syntax and semantics have been changed slightly.
The following are the symbols understood by regex and regcmp, and their meanings.
.
'') operations are
equivalent to {1,} and {0,} respectively.
'', ``+'', ``{}'',
can work on a single character or a regular
expression enclosed in parentheses.
For example, (a
(cb+)
)$0.
By necessity, all the above defined symbols are special. They must, therefore, be escaped with a \ (backslash) to be used as themselves.
char *cursor, *newcursor, *ptr;
...
newcursor = regex((ptr = regcmp("\n", (char *)0)), cursor);
free(ptr);
This example matches a leading new-line in the subject string
pointed at by cursor.
Example 2:
char ret0[9];
char *newcursor, *name;
...
name = regcmp("([A-Za-z][A-za-z0-9]{0,7})$0", (char *)0);
newcursor = regex(name, "012Testing345", ret0);
This example matches through the string ``Testing3'' and returns
the address of the character after the last matched character (the ``4'').
The string ``Testing3'' is copied to the character array ret0.
Example 3:
#include "file.i"
char *string, *newcursor;
...
newcursor = regex(name, string);
This example applies a precompiled regular expression in file.i
against
string.
See
regcmp(CP).
Example 4:
char *ptr, *newcursor;It is assumed in this example that the current locale's collation rules specify the following sequence:ptr = regcmp("[a-[=i=][:digit:]]*",(char*)0); newcursor = regex(ptr, "123CHICO321");
A,a,B,b,C,c,CH,Ch,ch,D,d,E,e,F,f,G,g,H,h,I,i.....The characters I and i are also both in the same ``primary'' collation group.
The following characters are all members of the digit ctype class:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9This example matches through the string ``123CHIC'' and returns the address of the character ``O'' in the string.