The Single UNIX ® Specification, Version 2
Copyright © 1997 The Open Group

 NAME

advance, compile, step, loc1, loc2, locs - compile and match regular expressions (LEGACY)

 SYNOPSIS



#define INIT declarations
#define GETC() getc code
#define PEEKC() peek code
#define UNGETC() ungetc code
#define RETURN(ptr) return code
#define ERROR(val) error code

#include <regexp.h>

char *compile(char *instring, char *expbuf,
    const char *endbuf, int eof);

int step(const char *string, const char *expbuf);

int advance(const char *string, const char *expbuf);

extern char *loc1, *loc2, *locs;

 DESCRIPTION

These are general-purpose, regular expression-matching functions to be used in programs that perform regular expression matching, using the Regular Expressions described in Simple Regular Expressions (Historical Version) . These functions are defined by the <regexp.h> header.

Implementations may also accept internationalised simple regular expressions as input.

Programs must have the following five macros declared before the #include <regexp.h> statement. These macros are used by compile(). The macros GETC(), PEEKC() and UNGETC() operate on the regular expression given as input to compile().

GETC()
This macro returns the value of the next character (byte) in the regular expression pattern. Successive calls to GETC() should return successive characters of the regular expression.
PEEKC()
This macro returns the next character (byte) in the regular expression. Immediately successive calls to PEEKC() should return the same byte, which should also be the next character returned by GETC().
UNGETC(c)
This macro causes the argument c to be returned by the next call to GETC() and PEEKC(). No more than one character of pushback is ever needed and this character is guaranteed to be the last character read by GETC(). The value of the macro UNGETC(c) is always ignored.
RETURN(ptr)
This macro is used on normal exit of the compile() function. The value of the argument ptr is a pointer to the character after the last character of the compiled regular expression. This is useful to programs that have memory allocation to manage.
ERROR(val)
This macro is the abnormal return from compile(). The argument val is an error number (see the ERRORS section below for meanings). This call should never return.

The step() and advance() functions do pattern matching given a character string and a compiled regular expression as input.

The compile() function takes as input a simple regular expression (see Simple Regular Expressions (Historical Version) ) and produces a compiled expression that can be used with step() and advance().

The first parameter instring is never used explicitly by compile() 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 invoke functions to input characters or have characters in an external array can pass down (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.

Each program that includes the <regexp.h> header must have a #define statement for INIT. It is used for dependent declarations and initialisations. 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 the EXAMPLES section below.

The first parameter to step() 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 compile.

The step() function 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 advance() function 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 some time during the backing up process, advance() will break out of the loop that backs up and will return 0.

The external variables circf, sed and nbra are reserved.

 Simple Regular Expressions (Historical Version)
A Simple Regular Expression (SRE) specifies a set of character strings. A member of this set of strings is said to be matched by the SRE.

A pattern is constructed from one or more SREs. An SRE consists of ordinary characters or metacharacters.

Within a pattern, all alphanumeric characters that are not part of a bracket expression, back-reference or duplication match themselves; that is, the SRE pattern abc , when applied to a set of strings, will match only those strings containing the character sequence abc anywhere in them.

Most other characters also match themselves. However, a small set of characters, known as the metacharacters, have special meanings when encountered in patterns. They are described below.

 Simple Regular Expression Construction
SREs are constructed as follows:
Expression
Meaning
c
The character c, where c is not a special character.
\c
The character c, where c is any character with special meaning, see below.
^
The beginning of the string being compared.
$
The end of the string being compared.
.
Any character.
[s]
Any character in the non-empty set s, where s is a sequence of characters. Ranges may be specified as c-c. The character ] may be included in the set by placing it first in the set. The character "-" may be included in the set by placing it first or last in the set. The character "^" may be included in the set by placing it anywhere other than first in the set, see below. Ranges in Simple Regular Expressions are only valid if the LC_COLLATE category is set to the C locale. Otherwise, the effect of using the range notation is unspecified.
[^s]
Any character not in the set s, where s is defined as above.
r*
Zero or more successive occurrences of the regular expression r. The longest leftmost match is chosen.
rx
The occurrence of regular expression r followed by the occurrence of regular expression x. (Concatenation.)
r\{m,n\}
Any number of m through n successive occurrences of the regular expression r. The regular expression r\{m\} matches exactly m occurrences, r\{m,\} matches at least m occurrences. The maximum number of occurrences is matched.
\(r\)
The regular expression r. The \( and \) sequences are ignored.
\n
When \n (where n is a number in the range 1 to 9) appears in a concatenated regular expression, it stands for the regular expression x, where x is the nth regular expression enclosed in \( and \) sequences that appeared earlier in the concatenated regular expression. For example, in the pattern \(r\)x\(y the \2 matches the regular expression y, giving rxyzy.

Characters that have special meaning except where 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 or at the beginning of each of the strings to which the match is applied, 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 left square bracket or before the right square bracket, [-c ] or [c -], in which case it has no special meaning. The character "^" has the meaning complement of if it immediately follows the left square bracket, [^c ]. Elsewhere between brackets, [ c^], it stands for the ordinary character "^". The right square bracket (]) loses its special meaning and represents itself in a bracket expression if it occurs first in the list after any initial circumflex (^) character.

The special meaning of the "\" operator can be escaped only by preceding it with another "\"; that is, "\\".

 SRE Operator Precedence
The precedence of the operators is as shown below:
[...]
High precedence.
*
concatenation
Low precedence.
 Internationalised SREs
Character expressions within square brackets are constructed as follows:
Expression
Meaning
c
The single character c where c is not a special character.
[[:class:]]
A character class expression. Any character of type class, as defined by category LC_CTYPE in the program's locale (see the XBD specification, Locale ). For class, one of the following should be substituted:
alpha
A letter.
upper
An upper-case letter.
lower
A lower-case letter.
digit
A decimal digit.
xdigit
A hexadecimal digit.
alnum
An alphanumeric (letter or digit).
space
A character producing white space in displayed text.
punct
A punctuation character.
print
A printing character.
graph
A character with a visible representation.
cntrl
A control character.

[[=c=]]
An equivalence class. Any collation element defined as having the same relative order in the current collation sequence as c. As an example, if A and a belong to the same equivalence class, then both [[=A=]b] and [[ =a=]b] are equivalent to [ Aab].

[[.cc.]]
A collating symbol. Multi-character collating elements must be represented as collating symbols to distinguish them from single-character collating elements. As an example, if the string ch is a valid collating element, then [[ .ch.]] will be treated as an element matching the same string of characters, while ch will be treated as a simple list of c and h. If the string is not a valid collating element in the current collating sequence definition, the symbol will be treated as an invalid expression.

[c-c]
Any collation element in the character expression range c-c, where c can identify a collating symbol or an equivalence class. If the character "-" appears immediately after an opening square bracket (for example, [-c]) or immediately prior to a closing square bracket (for example, [c-]), it has no special meaning.

^
Immediately following an opening square bracket, means the complement of, for example, [^c]. Otherwise, it has no special meaning.

Within square brackets, a "." that is not part of a [[ .cc.]] sequence, or a ":" that is not part of a [[:class:]] sequence, or an "=" that is not part of a [[=c=]] sequence, matches itself.

 SRE Examples
Below are examples of regular expressions:

Pattern Meaning
ab.d ab any character d
ab.*d ab any sequence of characters (including none) d
ab[xyz]d ab one of x y or z d
ab[c]d ab anything except c d
abcd$ a line containing only abcd
[a-d] any one of a b c or d

These interfaces need not be reentrant.

 RETURN VALUE

The compile() function uses the macro RETURN() on success and the macro ERROR() on failure, see above. The step() and advance() functions return non-zero on a successful match and 0 if there is no match.

 ERRORS

11
Range endpoint too large.
16
Bad number.
25
\digit out of range.
36
Illegal or missing delimiter.
41
No remembered search string.
42
\( \) imbalance.
43
Too many \( .
44
More than two numbers given in \{ \} .
45
} expected after \ .
46
First number exceeds second in \{ \} .
49
[ ] imbalance.
50
Regular expression overflow.

 EXAMPLES

The following is an example of how the regular expression macros and calls might be defined by an application program:

#define INIT         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();

 APPLICATION USAGE

Applications should migrate to the fnmatch(), glob(), regcomp() and regexec() functions which provide full internationalised regular expression functionality compatible with the ISO POSIX-2 standard, as described in the XBD specification, Regular Expressions .

 FUTURE DIRECTIONS

None.

 SEE ALSO

fnmatch(), glob(), regcomp(), regexec(), setlocale(), <regex.h>, <regexp.h>, the XBD specification, Regular Expressions .

DERIVATION

Derived from Issue 2 of the SVID.

UNIX ® is a registered Trademark of The Open Group.
Copyright © 1997 The Open Group
[ Main Index | XSH | XCU | XBD | XCURSES | XNS ]