Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

regexp: advance, compile, step, loc1, loc2, locs - compile and match regular expressions

&pagelevel(4)&pagelevel

Syntax

#define INIT declarations
#define GETC () getc code
#define PEEKC() peekc 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 functions are general-purpose functions for handling regular expressions in programs that perform pattern matching for regular expressions. They are defined in the header regexp.h.

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

GETC()

returns the value of the next character (byte) in the regular
expression pattern. The user must ensure that successive calls to
GETC() return successive characters of the regular expression.

PEEKC()

returns the next character (byte) in the regular expression. The user
must ensure that immediately successive calls to PEEKC() return
the same byte, which should also be identical to the next character
returned by GETC().

UNGETC( c )

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 )

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 )

corresponds to the abnormal termination of the compile()
function. The argument val is an error number (see the Errors
section below for the meanings of individual return values). The
user must ensure that this call never returns.

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 and produces a compiled expression that can be used with step() and advance().

The syntax of the compile() function is as follows:

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

  • 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 which process characters in an external array can pass down the value (char*)0 for this parameter.

  • The next parameter, expbuf, is a pointer to char. 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 contains the #include statement for regexp.h must also have a #define statement for the INIT macro. This macro 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().

The step() and advance() functions have two parameters each:

  • The first parameter, string, is a pointer to a string of characters to be checked against a regular expression. This string must be terminated with a null byte.

  • The second parameter, expbuf, is the compiled regular expression which was obtained by a call to compile().

step() returns a non-zero value if some substring of string matches the regular expression in expbuf, and it returns the value 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 byte at the end of string.

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.

If the advance() function encounters an * character or the character 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 test whether the pattern sought is already contained in the previously matched substring by backing 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.

Syntax elements for constructing patterns:

Regular
expr.

Meaning

Example

Matching string

r+

One or more occurrences of the regular
expression r. r must be in one of the following
forms: r, \ r, any character, [ r ], [ r1-r2 ], [^ s ],

[^ r1-r2 ], ( r ), ( r1 | r2 )

u+

u, uu, uuu, ...

r?

Zero or one occurrence of the regular
expression r. r must be in one of the following
forms: r, \ r, any character, [ r ], [ r1-r2 ], [^ s ],

[^ r1-r2 ], ( r ), ( r1 | r2 )

u?

none or u

( r )

Strings matching regular expression -r can be any expression.

(ok(abc))

(au)*

okabc
none or
aus, auau, ...

( r1 | r2 )

Strings matching regular expression r1 or r2.

(ok|ko)

ok or ko

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

Only some of the characters, known as metacharacters, have a special meaning when used in regular expressions. The other characters match themselves.The regular
expressions that may be used in regexp functions are constructed as follows:

Expression

Meaning

c                     

The character c, where c must not be a special character.

\ c

The character c, where c is any character other than a digit in the range 1-9.

^

The beginning of the line being compared.

$

The end of the line being compared.

.

Any character in the input.

[s]

Any character in the set s, where s is a sequence of characters. Ranges may be specified as [ c-c ]. The character ] may be included in the set only in the first position; the character - may be included only in the first or last position, and the character ^ may be included by placing it anywhere other than first position in the set. Ranges in SREs are only valid if the LC_COLLATE category is set to the C locale.

[^ 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 matching string is used.

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 is a number in the range 1-9 and appears in a concatenated regular expression, it stands for the regular expression x, where x is the n-th 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.

The following characters have special meaning when they do not appear within square brackets [ ] or are preceded by a \ (backslash): ., *, [, \.
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

[...]

high precedence

concatenation

low precedence

Internationalized SREs

Character expressions within square brackets are constructed as follows:

c

A single character c, where c is not a special character.

[[:class:]]

A char class expression. Any character of type class, as defined by category LC_CTYPE in the program ́s locale (see the manual "POSIX
Commands" [ 2 (Related publications) ])


One of the following may be substituted for class:

alpha

a letter

upper

an uppercase letter

lower

a lowercase letter

digit

a decimal digit

xdigit

a hexadecimal digit

alnum

an alphanumeric character (letter or digit)

space

a blank

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 [[.cc.]] 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 ch 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.

In the case of expressions 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.

Examples of regular expressions

ab.d

ab any character d

ab.*d

ab any sequence of characters (including none) d

ab[xyz]d

ab one of the characters x y or z d

ab[^c]d

ab any character, except c d

^abcd$

a line containing only abcd

a-d

any one of the characters a b c or d

Returnwert

RETURN()

when compile() is successful.


!= 0

when step() and advance() are successful.


ERROR

if compile() fails.


0

if step() and advance() fail.

Errors

11

Range endpoint too large


16

Invalid number


25

\digit out of range


36

Illegal or missing delimiter


41

No remembered search string in memory


42

\(\) imbalance


43

Too many \(


44

More than two numbers given in \{\}


45

} expected after \


46

First number exceeds second in \{\}


49

[ ] 


50

Regular expression overflow

See also

fnmatch(), glob(), regcomp(), regexec(), stlocale(), regex.h, regexp.h, and the manual "POSIX Commands" [ 2 ].