BSD 4_4 development
[unix-history] / usr / share / man / cat3 / regerror.0
REGEXP(3) BSD Programmer's Manual REGEXP(3)
N\bNA\bAM\bME\bE
r\bre\beg\bgc\bco\bom\bmp\bp, r\bre\beg\bge\bex\bxe\bec\bc, r\bre\beg\bgs\bsu\bub\bb, r\bre\beg\bge\ber\brr\bro\bor\br - regular expression handlers
S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS
#\b#i\bin\bnc\bcl\blu\bud\bde\be <\b<r\bre\beg\bge\bex\bxp\bp.\b.h\bh>\b>
_\br_\be_\bg_\be_\bx_\bp _\b*
r\bre\beg\bgc\bco\bom\bmp\bp(_\bc_\bo_\bn_\bs_\bt _\bc_\bh_\ba_\br _\b*_\be_\bx_\bp);
_\bi_\bn_\bt
r\bre\beg\bge\bex\bxe\bec\bc(_\bc_\bo_\bn_\bs_\bt _\br_\be_\bg_\be_\bx_\bp _\b*_\bp_\br_\bo_\bg, _\bc_\bo_\bn_\bs_\bt _\bc_\bh_\ba_\br _\b*_\bs_\bt_\br_\bi_\bn_\bg);
_\bv_\bo_\bi_\bd
r\bre\beg\bgs\bsu\bub\bb(_\bc_\bo_\bn_\bs_\bt _\br_\be_\bg_\be_\bx_\bp _\b*_\bp_\br_\bo_\bg, _\bc_\bo_\bn_\bs_\bt _\bc_\bh_\ba_\br _\b*_\bs_\bo_\bu_\br_\bc_\be, _\bc_\bh_\ba_\br _\b*_\bd_\be_\bs_\bt);
D\bDE\bES\bSC\bCR\bRI\bIP\bPT\bTI\bIO\bON\bN
This interface is made obsolete by regex(3).
The r\bre\beg\bgc\bco\bom\bmp\bp(), r\bre\beg\bge\bex\bxe\bec\bc(), r\bre\beg\bgs\bsu\bub\bb(), and r\bre\beg\bge\ber\brr\bro\bor\br() functions implement
egrep(1)-style regular expressions and supporting facilities.
The r\bre\beg\bgc\bco\bom\bmp\bp() function compiles a regular expression into a structure of
type regexp, and returns a pointer to it. The space has been allocated
using malloc(3) and may be released by free.
The r\bre\beg\bge\bex\bxe\bec\bc() function matches a NUL-terminated _\bs_\bt_\br_\bi_\bn_\bg against the com-
piled regular expression in _\bp_\br_\bo_\bg. It returns 1 for success and 0 for
failure, and adjusts the contents of _\bp_\br_\bo_\bg's _\bs_\bt_\ba_\br_\bt_\bp and _\be_\bn_\bd_\bp (see below)
accordingly.
The members of a regexp structure include at least the following (not
necessarily in order):
char *startp[NSUBEXP];
char *endp[NSUBEXP];
where NSUBEXP is defined (as 10) in the header file. Once a successful
r\bre\beg\bge\bex\bxe\bec\bc() has been done using the r\bre\beg\bge\bex\bxp\bp(), each _\bs_\bt_\ba_\br_\bt_\bp- _\be_\bn_\bd_\bp pair de-
scribes one substring within the _\bs_\bt_\br_\bi_\bn_\bg, with the _\bs_\bt_\ba_\br_\bt_\bp pointing to the
first character of the substring and the _\be_\bn_\bd_\bp pointing to the first char-
acter following the substring. The 0th substring is the substring of
_\bs_\bt_\br_\bi_\bn_\bg that matched the whole regular expression. The others are those
substrings that matched parenthesized expressions within the regular ex-
pression, with parenthesized expressions numbered in left-to-right order
of their opening parentheses.
The r\bre\beg\bgs\bsu\bub\bb() function copies _\bs_\bo_\bu_\br_\bc_\be to _\bd_\be_\bs_\bt, making substitutions accord-
ing to the most recent r\bre\beg\bge\bex\bxe\bec\bc() performed using _\bp_\br_\bo_\bg. Each instance of
`&' in _\bs_\bo_\bu_\br_\bc_\be is replaced by the substring indicated by _\bs_\bt_\ba_\br_\bt_\bp[] and
_\be_\bn_\bd_\bp[]. Each instance of `\_\bn', where _\bn is a digit, is replaced by the
substring indicated by _\bs_\bt_\ba_\br_\bt_\bp[_\bn] and _\be_\bn_\bd_\bp[_\bn]. To get a literal `&' or
`\_\bn' into _\bd_\be_\bs_\bt, prefix it with `\'; to get a literal `\' preceding `&' or
`\_\bn', prefix it with another `\'.
The r\bre\beg\bge\ber\brr\bro\bor\br() function is called whenever an error is detected in
r\bre\beg\bgc\bco\bom\bmp\bp(), r\bre\beg\bge\bex\bxe\bec\bc(), or r\bre\beg\bgs\bsu\bub\bb(). The default r\bre\beg\bge\ber\brr\bro\bor\br() writes the
string _\bm_\bs_\bg, with a suitable indicator of origin, on the standard error
output and invokes exit(2). The r\bre\beg\bge\ber\brr\bro\bor\br() function can be replaced by
the user if other actions are desirable.
R\bRE\bEG\bGU\bUL\bLA\bAR\bR E\bEX\bXP\bPR\bRE\bES\bSS\bSI\bIO\bON\bN S\bSY\bYN\bNT\bTA\bAX\bX
A regular expression is zero or more _\bb_\br_\ba_\bn_\bc_\bh_\be_\bs, separated by `|'. It
matches anything that matches one of the branches.
A branch is zero or more _\bp_\bi_\be_\bc_\be_\bs, concatenated. It matches a match for
the first, followed by a match for the second, etc.
A piece is an _\ba_\bt_\bo_\bm possibly followed by `*', `+', or `?'. An atom fol-
lowed by `*' matches a sequence of 0 or more matches of the atom. An
atom followed by `+' matches a sequence of 1 or more matches of the atom.
An atom followed by `?' matches a match of the atom, or the null string.
An atom is a regular expression in parentheses (matching a match for the
regular expression), a _\br_\ba_\bn_\bg_\be (see below), `.' (matching any single char-
acter), `^' (matching the null string at the beginning of the input
string), `$' (matching the null string at the end of the input string), a
`\' followed by a single character (matching that character), or a single
character with no other significance (matching that character).
A _\br_\ba_\bn_\bg_\be is a sequence of characters enclosed in `[]'. It normally match-
es any single character from the sequence. If the sequence begins with
`^', it matches any single character _\bn_\bo_\bt from the rest of the sequence.
If two characters in the sequence are separated by `-', this is shorthand
for the full list of ASCII characters between them (e.g. `[0-9]' matches
any decimal digit). To include a literal `]' in the sequence, make it
the first character (following a possible `^'). To include a literal
`-', make it the first or last character.
A\bAM\bMB\bBI\bIG\bGU\bUI\bIT\bTY\bY
If a regular expression could match two different parts of the input
string, it will match the one which begins earliest. If both begin in
the same place but match different lengths, or match the same length in
different ways, life gets messier, as follows.
In general, the possibilities in a list of branches are considered in
left-to-right order, the possibilities for `*', `+', and `?' are consid-
ered longest-first, nested constructs are considered from the outermost
in, and concatenated constructs are considered leftmost-first. The match
that will be chosen is the one that uses the earliest possibility in the
first choice that has to be made. If there is more than one choice, the
next will be made in the same manner (earliest possibility) subject to
the decision on the first choice. And so forth.
For example, `(ab|a)b*c' could match `abc' in one of two ways. The first
choice is between `ab' and `a'; since `ab' is earlier, and does lead to a
successful overall match, it is chosen. Since the `b' is already spoken
for, the `b*' must match its last possibility--the empty string--since it
must respect the earlier choice.
In the particular case where no `|'s are present and there is only one
`*', `+', or `?', the net effect is that the longest possible match will
be chosen. So `ab*', presented with `xabbbby', will match `abbbb'. Note
that if `ab*', is tried against `xabyabbbz', it will match `ab' just af-
ter `x', due to the begins-earliest rule. (In effect, the decision on
where to start the match is the first choice to be made, hence subsequent
choices must respect it even if this leads them to less-preferred alter-
natives.)
R\bRE\bET\bTU\bUR\bRN\bN V\bVA\bAL\bLU\bUE\bES\bS
The r\bre\beg\bgc\bco\bom\bmp\bp() function returns NULL for a failure (r\bre\beg\bge\ber\brr\bro\bor\br() permit-
ting), where failures are syntax errors, exceeding implementation limits,
or applying `+' or `*' to a possibly-null operand.
S\bSE\bEE\bE A\bAL\bLS\bSO\bO
ed(1), ex(1), expr(1), egrep(1), fgrep(1), grep(1), regex(3)
H\bHI\bIS\bST\bTO\bOR\bRY\bY
Both code and manual page for r\bre\beg\bgc\bco\bom\bmp\bp(), r\bre\beg\bge\bex\bxe\bec\bc(), r\bre\beg\bgs\bsu\bub\bb(), and
r\bre\beg\bge\ber\brr\bro\bor\br() were written at the University of Toronto and appeared in
4.3BSD-Tahoe. They are intended to be compatible with the Bell V8
regexp(3), but are not derived from Bell code.
B\bBU\bUG\bGS\bS
Empty branches and empty regular expressions are not portable to V8.
The restriction against applying `*' or `+' to a possibly-null operand is
an artifact of the simplistic implementation.
Does not support egrep's newline-separated branches; neither does the V8
regexp(3), though.
Due to emphasis on compactness and simplicity, it's not strikingly fast.
It does give special attention to handling simple cases quickly.
4.4BSD June 4, 1993 3