REGEXP(7) | Standards, Environments, and Macros | REGEXP(7) |
regexp, compile, step, advance - simple regular expression compile and match routines
#define INIT declarations #define GETC(void) getc code #define PEEKC(void) peekc code #define UNGETC(void) ungetc code #define RETURN(ptr) return code #define ERROR(val) error code extern char *loc1, *loc2, *locs; #include <regexp.h> char *compile(char *instring, char *expbuf, const char *endfug, int eof);
int step(const char *string, const char *expbuf);
int advance(const char *string, const char *expbuf);
Regular Expressions (REs) provide a mechanism to select specific strings from a set of character strings. The Simple Regular Expressions described below differ from the Internationalized Regular Expressions described on the regex(7) manual page in the following ways:
The functions step(), advance(), and compile() are general purpose regular expression matching routines to be used in programs that perform regular expression matching. These functions are defined by the <regexp.h> header.
The functions step() and advance() do pattern matching given a character string and a compiled regular expression as input.
The function compile() takes as input a regular expression as defined below and produces a compiled expression that can be used with step() or advance().
A regular expression specifies a set of character strings. A member of this set of strings is said to be matched by the regular expression. Some characters have special meaning when used in a regular expression; other characters stand for themselves.
The following one-character REs match a single character:
1.1
1.2
a.
b.
c.
d.
1.3
1.4
The following rules may be used to construct REs from one-character REs:
2.1
2.2
2.3
2.4
2.5
2.6
An RE may be constrained to match words.
3.1
3.2
An entire RE may be constrained to match only an initial segment or final segment of a line (or both).
4.1
4.2
4.3
The null RE (for example, //) is equivalent to the last RE encountered.
Addresses are constructed as follows:
Characters that have special meaning except when they appear within square brackets ([]) or are preceded by \ are: ., *, [, \. Other special characters, such as $ have special meaning in more restricted contexts.
The character ^ at the beginning of an expression permits a successful match only immediately after a newline, and the character $ at the end of an expression requires a trailing newline.
Two characters have special meaning only when used within square brackets. The character - denotes a range, [c-c], unless it is just after the open bracket or before the closing bracket, [-c] or [c-] in which case it has no special meaning. When used within brackets, the character ^ has the meaning complement of if it immediately follows the open bracket (example: [^c]); elsewhere between brackets (example: [c^]) it stands for the ordinary character ^.
The special meaning of the \ operator can be escaped only by preceding it with another \, for example \\.
Programs must have the following five macros declared before the #include <regexp.h> statement. These macros are used by the compile() routine. The macros GETC, PEEKC, and UNGETC operate on the regular expression given as input to compile().
GETC
PEEKC
UNGETC
RETURN(ptr)
ERROR(val)
The syntax of the compile() routine is as follows:
compile(instring, expbuf, endbuf, eof)
The first parameter, instring, is never used explicitly by the compile() routine but is useful for programs that pass down different pointers to input characters. It is sometimes used in the INIT declaration (see below). Programs which call functions to input characters or have characters in an external array can pass down a value of (char *)0 for this parameter.
The next parameter, expbuf, is a character pointer. It points to the place where the compiled regular expression will be placed.
The parameter endbuf is one more than the highest address where the compiled regular expression may be placed. If the compiled expression cannot fit in (endbuf-expbuf) bytes, a call to ERROR(50) is made.
The parameter eof is the character which marks the end of the regular expression. This character is usually a /.
Each program that includes the <regexp.h> header file must have a #define statement for INIT. It is used for dependent declarations and initializations. Most often it is used to set a register variable to point to the beginning of the regular expression so that this register variable can be used in the declarations for GETC, PEEKC, and UNGETC. Otherwise it can be used to declare external variables that might be used by GETC, PEEKC and UNGETC. (See EXAMPLES below.)
The first parameter to the step() and advance() functions is a pointer to a string of characters to be checked for a match. This string should be null terminated.
The second parameter, expbuf, is the compiled regular expression which was obtained by a call to the function compile().
The function step() returns non-zero if some substring of string matches the regular expression in expbuf and 0 if there is no match. If there is a match, two external character pointers are set as a side effect to the call to step(). The variable loc1 points to the first character that matched the regular expression; the variable loc2 points to the character after the last character that matches the regular expression. Thus if the regular expression matches the entire input string, loc1 will point to the first character of string and loc2 will point to the null at the end of string.
The function advance() returns non-zero if the initial substring of string matches the regular expression in expbuf. If there is a match, an external character pointer, loc2, is set as a side effect. The variable loc2 points to the next character in string after the last character that matched.
When advance() encounters a * or \{ \} sequence in the regular expression, it will advance its pointer to the string to be matched as far as possible and will recursively call itself trying to match the rest of the string to the rest of the regular expression. As long as there is no match, advance() will back up along the string until it finds a match or reaches the point in the string that initially matched the * or \{ \}. It is sometimes desirable to stop this backing up before the initial point in the string is reached. If the external character pointer locs is equal to the point in the string at sometime during the backing up process, advance() will break out of the loop that backs up and will return zero.
The external variables circf, sed, and nbra are reserved.
Example 1 Using Regular Expression Macros and Calls
The following is an example of how the regular expression macros and calls might be defined by an application program:
#define INIT register char *sp = instring; #define GETC() (*sp++) #define PEEKC() (*sp) #define UNGETC(c) (--sp) #define RETURN(c) return; #define ERROR(c) regerr() #include <regexp.h> ...
(void) compile(*argv, expbuf, &expbuf[ESIZE],'\0'); ...
if (step(linebuf, expbuf))
succeed;
The function compile() uses the macro RETURN on success and the macro ERROR on failure (see above). The functions step() and advance() return non-zero on a successful match and zero if there is no match. Errors are:
11
16
25
36
41
42
43
44
45
46
49
50
February 17, 2023 | OmniOS |