Module Name: src
Committed By: uwe
Date: Tue Sep 24 14:10:44 UTC 2024
Modified Files:
src/lib/libc/regex: regex.3
Log Message:
regex.3: brush up markup
Use \N for backreferences in both places for consistency and to make
it more obviously different from \n (besides the different fonts, that
might not be too obvious).
Use \&. instead of .\& to protect punctuation.
To generate a diff of this commit:
cvs rdiff -u -r1.34 -r1.35 src/lib/libc/regex/regex.3
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
Modified files:
Index: src/lib/libc/regex/regex.3
diff -u src/lib/libc/regex/regex.3:1.34 src/lib/libc/regex/regex.3:1.35
--- src/lib/libc/regex/regex.3:1.34 Sun Sep 22 00:22:08 2024
+++ src/lib/libc/regex/regex.3 Tue Sep 24 14:10:43 2024
@@ -1,4 +1,4 @@
-.\" $NetBSD: regex.3,v 1.34 2024/09/22 00:22:08 christos Exp $
+.\" $NetBSD: regex.3,v 1.35 2024/09/24 14:10:43 uwe Exp $
.\"
.\" Copyright (c) 1992, 1993, 1994 Henry Spencer.
.\" Copyright (c) 1992, 1993, 1994
@@ -37,6 +37,7 @@
.Dd September 21, 2024
.Dt REGEX 3
.Os
+.
.Sh NAME
.Nm regcomp ,
.Nm regexec ,
@@ -44,36 +45,64 @@
.Nm regfree ,
.Nm regasub ,
.Nm regnsub
+.
.Nd regular-expression library
+.
.Sh LIBRARY
.Lb libc
+.
.Sh SYNOPSIS
+.
.In regex.h
+.
.Ft int
.Fo regcomp
-.Fa "regex_t * restrict preg" "const char * restrict pattern" "int cflags"
+.Fa "regex_t * restrict preg"
+.Fa "const char * restrict pattern"
+.Fa "int cflags"
.Fc
+.
.Ft int
.Fo regexec
-.Fa "const regex_t * restrict preg" "const char * restrict string"
-.Fa "size_t nmatch" "regmatch_t pmatch[restrict]" "int eflags"
+.Fa "const regex_t * restrict preg"
+.Fa "const char * restrict string"
+.Fa "size_t nmatch"
+.Fa "regmatch_t pmatch[restrict]"
+.Fa "int eflags"
.Fc
.Ft size_t
.Fo regerror
-.Fa "int errcode" "const regex_t * restrict preg"
-.Fa "char * restrict errbuf" "size_t errbuf_size"
+.Fa "int errcode"
+.Fa "const regex_t * restrict preg"
+.Fa "char * restrict errbuf"
+.Fa "size_t errbuf_size"
.Fc
+.
.Ft void
.Fn regfree "regex_t *preg"
+.
.Ft ssize_t
-.Fn regnsub "char *buf" "size_t bufsiz" "const char *sub" "const regmatch_t *rm" "const char *str"
+.Fo regnsub
+.Fa "char *buf"
+.Fa "size_t bufsiz"
+.Fa "const char *sub"
+.Fa "const regmatch_t *rm"
+.Fa "const char *str"
+.Fc
+.
.Ft ssize_t
-.Fn regasub "char **buf" "const char *sub" "const regmatch_t *rm" "const char *sstr"
+.Fo regasub
+.Fa "char **buf"
+.Fa "const char *sub"
+.Fa "const regmatch_t *rm"
+.Fa "const char *sstr"
+.Fc
+.
.Sh DESCRIPTION
These routines implement
.St -p1003.2
regular expressions
-.Pq Do RE Dc Ns s ;
+.Pq Do Tn RE Dc Ns s ;
see
.Xr re_format 7 .
The
@@ -100,7 +129,7 @@ It also declares the four functions,
a type
.Ft regoff_t ,
and a number of constants with names starting with
-.Dq Dv REG_ .
+.Ql REG_ .
.Pp
The
.Fn regcomp
@@ -117,8 +146,11 @@ structure pointed to by
The
.Fa cflags
argument
-is the bitwise OR of zero or more of the following flags:
-.Bl -tag -width REG_EXTENDED
+is the bitwise
+.Em or
+of zero or more of the following flags:
+.Bl -tag -width Dv
+.
.It Dv REG_EXTENDED
Compile modern
.Pq Dq extended
@@ -127,11 +159,13 @@ rather than the obsolete
.Pq Dq basic
REs that
are the default.
+.
.It Dv REG_BASIC
This is a synonym for 0,
provided as a counterpart to
.Dv REG_EXTENDED
to improve readability.
+.
.It Dv REG_NOSPEC
Compile with recognition of all special characters turned off.
All characters are thus considered ordinary,
@@ -149,42 +183,49 @@ and
may not be used
in the same call to
.Fn regcomp .
+.
.It Dv REG_ICASE
-Compile for matching that ignores upper/lower case distinctions.
+Compile for matching that ignores upper\|/\^lower case distinctions.
See
.Xr re_format 7 .
+.
.It Dv REG_NOSUB
Compile for matching that need only report success or failure,
not what was matched.
+.
.It 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 [^
+.Ql \&[^
bracket expressions and
-.Ql .\&
+.Ql \&.
never match newline,
a
-.Ql ^\&
+.Ql \&^
anchor matches the null string after any newline in the string
in addition to its normal function,
and the
-.Ql $\&
+.Ql \&$
anchor matches the null string before any newline in the
string in addition to its normal function.
+.
.It Dv REG_PEND
The regular expression ends,
-not at the first NUL,
+not at the first
+.Tn NUL ,
but just before the character pointed to by the
-.Va re_endp
+.Fa re_endp
member of the structure pointed to by
.Fa preg .
The
-.Va re_endp
+.Fa re_endp
member is of type
.Ft "const char *" .
-This flag permits inclusion of NULs in the RE;
+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
@@ -194,43 +235,44 @@ caution in software intended to be porta
.It Dv REG_GNU
Include GNU-inspired extensions:
.Pp
-.Bl -tag -offset indent -width XX -compact
-.It \eN
+.Bl -tag -offset indent -width Ds -compact
+.It Ic \e Ns Ar N
Use backreference
-.Dv N
+.Ar N
where
-.Dv N
-is a single digit number between
-.Dv 1
-and
-.Dv 9 .
-.It \ea
+.Ar N
+is a single digit number between 1 and 9.
+.It Ic \ea
Visual Bell
-.It \eb
+.It Ic \eb
Match a position that is a word boundary.
-.It \eB
+.It Ic \eB
Match a position that is a not word boundary.
-.It \ef
+.It Ic \ef
Form Feed
-.It \en
+.It Ic \en
Line Feed
-.It \er
+.It Ic \er
Carriage return
-.It \es
-Alias for [[:space:]]
-.It \eS
-Alias for [^[:space:]]
-.It \et
+.It Ic \es
+Alias for
+.Ql [[:space:]]
+.It Ic \eS
+Alias for
+.Ql [^[:space:]]
+.It Ic \et
Horizontal Tab
-.It \ev
+.It Ic \ev
Vertical Tab
-.It \ew
-Alias for [[:alnum:]_]
-.It \eW
-Alias for [^[:alnum:]_]
-.It \e'
+.It Ic \ew
+Alias for
+.Ql [[:alnum:]_]
+.It Ic \eW
+Alias for
+.Ql [^[:alnum:]_]
+.It Ic \e'
Matches the end of the subject string (the string to be matched).
-.It \e`
+.It Ic \e`
Matches the beginning of the subject string.
.El
.Pp
@@ -246,16 +288,17 @@ When successful,
returns 0 and fills in the structure pointed to by
.Fa preg .
One member of that structure
-(other than
-.Va re_endp )
+.Pq other than Fa re_endp
is publicized:
-.Va re_nsub ,
+.Fa re_nsub ,
of type
.Ft size_t ,
contains the number of parenthesized subexpressions within the RE
-(except that the value of this member is undefined if the
+.Po
+except that the value of this member is undefined if the
.Dv REG_NOSUB
-flag was used).
+flag was used
+.Pc .
If
.Fn regcomp
fails, it returns a non-zero error code;
@@ -288,13 +331,16 @@ is considered to be the text of an entir
newline.
The
.Fa eflags
-argument is the bitwise OR of zero or more of the following flags:
-.Bl -tag -width REG_STARTEND
+argument is the bitwise
+.Em or
+of zero or more of the following flags:
+.Bl -tag -width Dv
+.
.It Dv REG_NOTBOL
The first character of the string is treated as the continuation
of a line.
This means that the anchors
-.Ql ^\& ,
+.Ql \&^ ,
.Ql [[:<:]] ,
and
.Ql \e<
@@ -303,21 +349,25 @@ do not match before it; but see
below.
This does not affect the behavior of newlines under
.Dv REG_NEWLINE .
+.
.It Dv REG_NOTEOL
The NUL terminating
the string
does not end a line, so the
-.Ql $\&
+.Ql \&$
anchor does not match before it.
This does not affect the behavior of newlines under
.Dv REG_NEWLINE .
+.
.It Dv REG_STARTEND
The string is considered to start at
-.Fa string No +
-.Fa pmatch Ns [0]. Ns Fa rm_so
+.Sm off
+.Fa string Li " + " Fa pmatch Li [0]. Fa rm_so
+.Sm on
and to end before the byte located at
-.Fa string No +
-.Fa pmatch Ns [0]. Ns Fa rm_eo ,
+.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
@@ -335,7 +385,7 @@ Without
the position
.Fa rm_so
is considered the beginning of a line, such that
-.Ql ^
+.Ql \&^
matches before it, and the beginning of a word if there is a word
character at this position, such that
.Ql [[:<:]]
@@ -353,7 +403,7 @@ is greater than 0, the preceding charact
If the preceding character is a newline and the regular expression was compiled
with
.Dv REG_NEWLINE ,
-.Ql ^
+.Ql \&^
matches before the string; if the preceding character is not a word character
but the string starts with a word character,
.Ql [[:<:]]
@@ -386,9 +436,12 @@ is 0,
.Fn regexec
ignores the
.Fa pmatch
-argument (but see below for the case where
+argument
+.Po
+but see below for the case where
.Dv REG_STARTEND
-is specified).
+is specified
+.Pc .
Otherwise,
.Fa pmatch
points to an array of
@@ -401,10 +454,12 @@ and
.Va rm_eo ,
both of type
.Ft regoff_t
-(a signed arithmetic type at least as large as an
+.Po
+a signed arithmetic type at least as large as an
.Ft off_t
and a
-.Ft ssize_t ) ,
+.Ft ssize_t
+.Pc ,
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
@@ -414,7 +469,9 @@ argument given to
An empty substring is denoted by equal offsets,
both indicating the character following the empty substring.
.Pp
-The 0th member of the
+The
+.No 0 Ap th
+member of the
.Fa pmatch
array is filled in to indicate what substring of
.Fa string
@@ -425,22 +482,28 @@ member
.Va i
reports subexpression
.Va i ,
-with subexpressions counted (starting at 1) by the order of their opening
-parentheses in the RE, left to right.
-Unused entries in the array (corresponding either to subexpressions that
+with subexpressions counted
+.Pq starting at 1
+by the order of their opening parentheses in the RE, left to right.
+Unused entries in the array
+.Po
+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,
+exist in the RE,
+that is,
.Va i
>
-.Fa preg Ns -> Ns Va re_nsub ) )
+.Fa preg Ns Li -> Ns Fa re_nsub
+.Pc
have both
-.Va rm_so
+.Fa rm_so
and
-.Va rm_eo
-set to -1.
+.Fa rm_eo
+set to \-1.
If a subexpression participated in the match several times,
the reported substring is the last one it matched.
-(Note, as an example in particular, that when the RE
+.Po
+Note, as an example in particular, that when the RE
.Ql "(b*)+"
matches
.Ql bbb ,
@@ -449,7 +512,8 @@ the parenthesized subexpression matches
and then
an infinite number of empty strings following the last
.Ql b ,
-so the reported substring is one of the empties.)
+so the reported substring is one of the empties.
+.Pc
.Pp
If
.Dv REG_STARTEND
@@ -457,11 +521,13 @@ is specified,
.Fa pmatch
must point to at least one
.Ft regmatch_t
-(even if
+.Po
+even if
.Fa nmatch
is 0 or
.Dv REG_NOSUB
-was specified),
+was specified
+.Pc ,
to hold the input offsets for
.Dv REG_STARTEND .
Use for output is still entirely controlled by
@@ -472,7 +538,7 @@ is 0 or
.Dv REG_NOSUB
was specified,
the value of
-.Fa pmatch Ns [0]
+.Fa pmatch Ns Li [0]
will not be changed by a successful
.Fn regexec .
.Pp
@@ -489,7 +555,7 @@ to a human-readable, printable message.
If
.Fa preg
is
-.No non\- Ns Dv NULL ,
+.Pf non- Dv NULL ,
the error code should have arisen from use of
the
.Ft regex_t
@@ -500,27 +566,30 @@ and if the error code came from
it should have been the result from the most recent
.Fn regcomp
using that
-.Ft regex_t .
-The
+.Ft regex_t
.Po
+the
.Fn regerror
may be able to supply a more detailed message using information
from the
-.Ft regex_t .
-.Pc
+.Ft regex_t
+.Pc .
The
.Fn regerror
function
places the NUL-terminated message into the buffer pointed to by
.Fa errbuf ,
-limiting the length (including the NUL) to at most
+limiting the length
+.Pq including the Tn NUL
+to at most
.Fa errbuf_size
bytes.
If the whole message will not fit,
as much of it as will fit before the terminating NUL is supplied.
In any case,
the returned value is the size of buffer needed to hold the whole
-message (including terminating NUL).
+message
+.Pq including terminating Tn NUL .
If
.Fa errbuf_size
is 0,
@@ -531,7 +600,9 @@ If the
.Fa errcode
given to
.Fn regerror
-is first ORed with
+is first
+.Em or Ap ed
+with
.Dv REG_ITOA ,
the
.Dq message
@@ -546,16 +617,16 @@ is
then
.Fa preg
shall be
-.No non\- Ns Dv NULL
+.Pf non- Dv NULL
and the
-.Va re_endp
+.Fa re_endp
member of the structure it points to
must point to the printable name of an error code;
in this case, the result in
.Fa errbuf
is the decimal digits of
the numeric value of the error code
-(0 if the name is not recognized).
+.Pq 0 if the name is not recognized .
.Dv REG_ITOA
and
.Dv REG_ATOI
@@ -594,10 +665,8 @@ functions perform substitutions using
.Xr sed 1
like syntax.
They return the length of the string that would have been created
-if there was enough space or
-.Dv \-1
-on error, setting
-.Dv errno .
+if there was enough space or \-1 on error, setting
+.Va errno .
The result
is being placed in
.Fa buf
@@ -609,12 +678,14 @@ The
.Fa sub
argument contains a substitution string which might refer to the first
9 regular expression strings using
-.Dq \e<n>
+.So Ic \e Ns Ar N Sc
to refer to the nth matched
item, or
-.Dq &
-(which is equivalent to
-.Dq \e0 )
+.Ql &
+.Po
+which is equivalent to
+.Ic \e0
+.Pc
to refer to the full match.
The
.Fa rm
@@ -654,13 +725,15 @@ that will run almost any system out of m
A backslashed character other than one specifically given a magic meaning
by
.St -p1003.2
-(such magic meanings occur only in obsolete
-.Bq Dq basic
-REs)
+.Po
+such magic meanings occur only in obsolete
+.Pq Dq basic
+REs
+.Pc
is taken as an ordinary character.
.Pp
Any unmatched
-.Ql [\&
+.Ql \&[
is a
.Dv REG_EBRACK
error.
@@ -672,23 +745,25 @@ The endpoint of one range cannot begin a
the limit on repetition counts in bounded repetitions, is 255.
.Pp
A repetition operator
-.Ql ( ?\& ,
-.Ql *\& ,
-.Ql +\& ,
-or bounds)
+.Po
+.Ql \&? ,
+.Ql \&* ,
+.Ql \&+ ,
+or bounds
+.Pc
cannot follow another
repetition operator.
A repetition operator cannot begin an expression or subexpression
or follow
-.Ql ^\&
+.Ql \&^
or
-.Ql |\& .
+.Ql \&| .
.Pp
-.Ql |\&
+.Ql \&|
cannot appear first or last in a (sub)expression or after another
-.Ql |\& ,
+.Ql \&| ,
i.e., an operand of
-.Ql |\&
+.Ql \&|
cannot be an empty subexpression.
An empty parenthesized subexpression,
.Ql "()" ,
@@ -697,17 +772,17 @@ empty (sub)string.
An empty string is not a legal RE.
.Pp
A
-.Ql {\&
+.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 {\&
+.Ql \&{
.Em not
followed by a digit is considered an ordinary character.
.Pp
-.Ql ^\&
+.Ql \&^
and
-.Ql $\&
+.Ql \&$
beginning and ending subexpressions in obsolete
.Pq Dq basic
REs are anchors, not ordinary characters.
@@ -718,7 +793,7 @@ and
.Fn regexec
include the following:
.Pp
-.Bl -tag -width REG_ECOLLATE -compact
+.Bl -tag -width ".Dv REG_ECOLLATE" -compact
.It Dv REG_NOMATCH
The
.Fn regexec
@@ -756,10 +831,10 @@ invalid character range in
.It Dv REG_ESPACE
ran out of memory
.It Dv REG_BADRPT
-.Ql ?\& ,
-.Ql *\& ,
+.Ql \&? ,
+.Ql \&* ,
or
-.Ql +\&
+.Ql \&+
operand invalid
.It Dv REG_EMPTY
empty (sub)expression
@@ -775,9 +850,11 @@ illegal byte sequence (bad multibyte cha
.Xr re_format 7
.Pp
.St -p1003.2 ,
-sections 2.8 (Regular Expression Notation)
+sections 2.8
+.Pq Regular Expression Notation
and
-B.5 (C Binding for Regular Expression Matching).
+B.5
+.Pq Tn C No Binding for Regular Expression Matching .
.Sh HISTORY
Originally written by
.An Henry Spencer .
@@ -841,10 +918,10 @@ Due to a mistake in
things like
.Ql "a)b"
are legal REs because
-.Ql )\&
+.Ql \&)
is
a special character only in the presence of a previous unmatched
-.Ql (\& .
+.Ql \&( .
This cannot be fixed until the spec is fixed.
.Pp
The standard's definition of back references is vague.