q Do Tn RE Dc Ns s ; see .Xr re_format 7 . The .Fn regcomp function compiles an RE written as a string into an internal form, .Fn regexec matches that internal form against a string and reports results, .Fn regerror transforms error codes from either into human-readable messages, and .Fn regfree frees any dynamically-allocated storage used by the internal form of an RE.
p The header n regex.h declares two structure types, .Ft regex_t and .Ft regmatch_t , the former for compiled internal forms and the latter for match reporting. It also declares the four functions, a type .Ft regoff_t , and a number of constants with names starting with .Ql REG_ .
p The .Fn regcomp function compiles the regular expression contained in the .Fa pattern string, subject to the flags in .Fa cflags , and places the results in the .Ft regex_t structure pointed to by .Fa preg . The .Fa cflags argument is the bitwise .Em or of zero or more of the following flags: l -tag -width Dv . t Dv REG_EXTENDED Compile modern
q Dq extended REs, rather than the obsolete
q Dq basic REs that are the default. . t Dv REG_BASIC This is a synonym for 0, provided as a counterpart to .Dv REG_EXTENDED to improve readability. . t Dv REG_NOSPEC Compile with recognition of all special characters turned off. All characters are thus considered ordinary, so the .Dq RE is a literal string. This is an extension, compatible with but not specified by .St -p1003.2 , and should be used with caution in software intended to be portable to other systems. .Dv REG_EXTENDED and .Dv REG_NOSPEC may not be used in the same call to .Fn regcomp . . t Dv REG_ICASE Compile for matching that ignores upper\|/\^lower case distinctions. See .Xr re_format 7 . . t Dv REG_NOSUB Compile for matching that need only report success or failure, not what was matched. . t Dv REG_NEWLINE Compile for newline-sensitive matching. By default, newline is a completely ordinary character with no special meaning in either REs or strings. With this flag, .Ql [^ bracket expressions and .Ql . never match newline, a .Ql ^ anchor matches the null string after any newline in the string in addition to its normal function, and the .Ql $ anchor matches the null string before any newline in the string in addition to its normal function. . t Dv REG_PEND The regular expression ends, not at the first .Tn NUL , but just before the character pointed to by the .Fa re_endp member of the structure pointed to by .Fa preg . The .Fa re_endp member is of type .Ft "const char *" . This flag permits inclusion of .Tn NUL Ns s in the RE; they are considered ordinary characters. This is an extension, compatible with but not specified by .St -p1003.2 , and should be used with caution in software intended to be portable to other systems. t Dv REG_GNU Include GNU-inspired extensions:
p l -tag -offset indent -width Ds -compact t Ic \e Ns Ar N Use backreference .Ar N where .Ar N is a single digit number between 1 and 9. t Ic \ea Visual Bell t Ic \eb Match a position that is a word boundary. t Ic \eB Match a position that is a not word boundary. t Ic \ef Form Feed t Ic \en Line Feed t Ic \er Carriage return t Ic \es Alias for .Ql [[:space:]] t Ic \eS Alias for .Ql [^[:space:]] t Ic \et Horizontal Tab t Ic \ev Vertical Tab t Ic \ew Alias for .Ql [[:alnum:]_] t Ic \eW Alias for .Ql [^[:alnum:]_] t Ic \e' Matches the end of the subject string (the string to be matched). t Ic \e` Matches the beginning of the subject string. .El
p This is an extension, compatible with but not specified by .St -p1003.2 , and should be used with caution in software intended to be portable to other systems. .El
p When successful, .Fn regcomp returns 0 and fills in the structure pointed to by .Fa preg . One member of that structure
q other than Fa re_endp is publicized: .Fa re_nsub , of type .Ft size_t , contains the number of parenthesized subexpressions within the RE
o except that the value of this member is undefined if the .Dv REG_NOSUB flag was used
c . If .Fn regcomp fails, it returns a non-zero error code; see .Sx RETURN VALUES .
p The .Fn regexec function matches the compiled RE pointed to by .Fa preg against the .Fa string , subject to the flags in .Fa eflags , and reports results using .Fa nmatch , .Fa pmatch , and the returned value. The RE must have been compiled by a previous invocation of .Fn regcomp . The compiled form is not altered during execution of .Fn regexec , so a single compiled RE can be used simultaneously by multiple threads.
p By default, the NUL-terminated string pointed to by .Fa string is considered to be the text of an entire line, minus any terminating newline. The .Fa eflags argument is the bitwise .Em or of zero or more of the following flags: l -tag -width Dv . t Dv REG_NOTBOL The first character of the string is treated as the continuation of a line. This means that the anchors .Ql ^ , .Ql [[:<:]] , and .Ql \e< do not match before it; but see .Dv REG_STARTEND below. This does not affect the behavior of newlines under .Dv REG_NEWLINE . . t Dv REG_NOTEOL The NUL terminating the string does not end a line, so the .Ql $ anchor does not match before it. This does not affect the behavior of newlines under .Dv REG_NEWLINE . . t Dv REG_STARTEND The string is considered to start at .Sm off .Fa string Li " + " Fa pmatch Li [0]. Fa rm_so .Sm on and to end before the byte located at .Sm off .Fa string Li " + " Fa pmatch Li [0]. Fa rm_eo , .Sm on regardless of the value of .Fa nmatch . See below for the definition of .Fa pmatch and .Fa nmatch . This is an extension, compatible with but not specified by .St -p1003.2 , and should be used with caution in software intended to be portable to other systems.
p Without .Dv REG_NOTBOL , the position .Fa rm_so is considered the beginning of a line, such that .Ql ^ matches before it, and the beginning of a word if there is a word character at this position, such that .Ql [[:<:]] and .Ql \e< match before it.
p With .Dv REG_NOTBOL , the character at position .Fa rm_so is treated as the continuation of a line, and if .Fa rm_so is greater than 0, the preceding character is taken into consideration. If the preceding character is a newline and the regular expression was compiled with .Dv REG_NEWLINE , .Ql ^ matches before the string; if the preceding character is not a word character but the string starts with a word character, .Ql [[:<:]] and .Ql \e< match before the string. .El
p See .Xr re_format 7 for a discussion of what is matched in situations where an RE or a portion thereof could match any of several substrings of .Fa string .
p Normally, .Fn regexec returns 0 for success and the non-zero code .Dv REG_NOMATCH for failure. Other non-zero error codes may be returned in exceptional situations; see .Sx RETURN VALUES .
p If .Dv REG_NOSUB was specified in the compilation of the RE, or if .Fa nmatch is 0, .Fn regexec ignores the .Fa pmatch argument
o but see below for the case where .Dv REG_STARTEND is specified
c . Otherwise, .Fa pmatch points to an array of .Fa nmatch structures of type .Ft regmatch_t . Such a structure has at least the members .Va rm_so and .Va rm_eo , both of type .Ft regoff_t
o a signed arithmetic type at least as large as an .Ft off_t and a .Ft ssize_t
c , containing respectively the offset of the first character of a substring and the offset of the first character after the end of the substring. Offsets are measured from the beginning of the .Fa string argument given to .Fn regexec . An empty substring is denoted by equal offsets, both indicating the character following the empty substring.
p The .No 0 Ap th member of the .Fa pmatch array is filled in to indicate what substring of .Fa string was matched by the entire RE. Remaining members report what substring was matched by parenthesized subexpressions within the RE; member .Va i reports subexpression .Va i , with subexpressions counted
q starting at 1 by the order of their opening parentheses in the RE, left to right. Unused entries in the array
o corresponding either to subexpressions that did not participate in the match at all, or to subexpressions that do not exist in the RE, that is, .Va i > .Fa preg Ns Li -> Ns Fa re_nsub
c have both .Fa rm_so and .Fa rm_eo set to -1. If a subexpression participated in the match several times, the reported substring is the last one it matched.
o Note, as an example in particular, that when the RE .Ql "(b*)+" matches .Ql bbb , the parenthesized subexpression matches each of the three .So Li b Sc Ns s and then an infinite number of empty strings following the last .Ql b , so the reported substring is one of the empties.
c
p If .Dv REG_STARTEND is specified, .Fa pmatch must point to at least one .Ft regmatch_t
o even if .Fa nmatch is 0 or .Dv REG_NOSUB was specified
c , to hold the input offsets for .Dv REG_STARTEND . Use for output is still entirely controlled by .Fa nmatch ; if .Fa nmatch is 0 or .Dv REG_NOSUB was specified, the value of .Fa pmatch Ns Li [0] will not be changed by a successful .Fn regexec .
p The .Fn regerror function maps a non-zero .Fa errcode from either .Fn regcomp or .Fn regexec to a human-readable, printable message. If .Fa preg is
f non- Dv NULL , the error code should have aris follow another repetition operator. A repetition operator cannot begin an expression or subexpression or follow .Ql ^ or .Ql | .
p .Ql | cannot appear first or last in a (sub)expression or after another .Ql | , i.e., an operand of .Ql | cannot be an empty subexpression. An empty parenthesized subexpression, .Ql "()" , is legal and matches an empty (sub)string. An empty string is not a legal RE.
p A .Ql { followed by a digit is considered the beginning of bounds for a bounded repetition, which must then follow the syntax for bounds. A .Ql { .Em not followed by a digit is considered an ordinary character.
p .Ql ^ and .Ql $ beginning and ending subexpressions in obsolete
q Dq basic REs are anchors, not ordinary characters. .Sh RETURN VALUES Non-zero error codes from .Fn regcomp and .Fn regexec include the following:
p l -tag -width ".Dv REG_ECOLLATE" -compact t Dv REG_NOMATCH The .Fn regexec function failed to match t Dv REG_BADPAT invalid regular expression t Dv REG_ECOLLATE invalid collating element t Dv REG_ECTYPE invalid character class t Dv REG_EESCAPE .Ql \e applied to unescapable character t Dv REG_ESUBREG invalid backreference number t Dv REG_EBRACK brackets .Ql "[ ]" not balanced t Dv REG_EPAREN parentheses .Ql "( )" not balanced t Dv REG_EBRACE braces .Ql "{ }" not balanced t Dv REG_BADBR invalid repetition count(s) in .Ql "{ }" t Dv REG_ERANGE invalid character range in .Ql "[ ]" t Dv REG_ESPACE ran out of memory t Dv REG_BADRPT .Ql ? , .Ql * , or .Ql + operand invalid t Dv REG_EMPTY empty (sub)expression t Dv REG_ASSERT cannot happen - you found a bug t Dv REG_INVARG invalid argument, e.g. negative-length string t Dv REG_ILLSEQ illegal byte sequence (bad multibyte character) .El .Sh SEE ALSO .Xr grep 1 , .Xr re_format 7
p .St -p1003.2 , sections 2.8
q Regular Expression Notation and B.5
q Tn C No Binding for Regular Expression Matching . .Sh HISTORY Originally written by .An Henry Spencer . Altered for inclusion in the x 4.4 distribution.
p The .Fn regnsub and .Fn regasub functions appeared in .Nx 8 . .Sh BUGS This is an alpha release with known defects. Please report problems.
p The back-reference code is subtle and doubts linger about its correctness in complex cases.
p The .Fn regexec function performance is poor. This will improve with later releases. The .Fa nmatch argument exceeding 0 is expensive; .Fa nmatch exceeding 1 is worse. The .Fn regexec function is largely insensitive to RE complexity .Em except that back references are massively expensive. RE length does matter; in particular, there is a strong speed bonus for keeping RE length under about 30 characters, with most special characters counting roughly double.
p The .Fn regcomp function implements bounded repetitions by macro expansion, which is costly in time and space if counts are large or bounded repetitions are nested. An RE like, say, .Ql "((((a{1,100}){1,100}){1,100}){1,100}){1,100}" will (eventually) run almost any existing machine out of swap space.
p There are suspected problems with response to obscure error conditions. Notably, certain kinds of internal overflow, produced only by truly enormous REs or by multiply nested bounded repetitions, are probably not handled well.
p Due to a mistake in .St -p1003.2 , things like .Ql "a)b" are legal REs because .Ql ) is a special character only in the presence of a previous unmatched .Ql ( . This cannot be fixed until the spec is fixed.
p The standard's definition of back references is vague. For example, does .Ql "a\e(\e(b\e)*\e2\e)*d" match .Ql "abbbd" ? Until the standard is clarified, behavior in such cases should not be relied on.
p The implementation of word-boundary matching is a bit of a kludge, and bugs may lurk in combinations of word-boundary matching and anchoring.
p Word-boundary matching does not work properly in multibyte locales.