From 7edb9f69d2426b044fa60c7a168c9eaeb12f1884 Mon Sep 17 00:00:00 2001 From: Yuri Pankov Date: Thu, 8 Jun 2017 20:42:56 +0300 Subject: [PATCH] 6431 document FreeBSD regex extensions Reviewed by: Robert Mustacchi Approved by: Dan McDonald --- usr/src/man/man3c/regcomp.3c | 1358 +++++++++++++++++-------------- usr/src/man/man5/regex.5 | 1810 ++++++++++++++++++++++++------------------ 2 files changed, 1805 insertions(+), 1363 deletions(-) rewrite usr/src/man/man3c/regcomp.3c (92%) rewrite usr/src/man/man5/regex.5 (84%) diff --git a/usr/src/man/man3c/regcomp.3c b/usr/src/man/man3c/regcomp.3c dissimilarity index 92% index 1b893e3531..bd2db58ae9 100644 --- a/usr/src/man/man3c/regcomp.3c +++ b/usr/src/man/man3c/regcomp.3c @@ -1,594 +1,764 @@ -'\" te -.\" Copyright (c) 1992, X/Open Company Limited. All Rights Reserved. Portions Copyright (c) 2003, Sun Microsystems, Inc. All Rights Reserved. -.\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for permission to reproduce portions of its copyrighted documentation. Original documentation from The Open Group can be obtained online at -.\" http://www.opengroup.org/bookstore/. -.\" The Institute of Electrical and Electronics Engineers and The Open Group, have given us permission to reprint portions of their documentation. In the following statement, the phrase "this text" refers to portions of the system documentation. Portions of this text are reprinted and reproduced in electronic form in the Sun OS Reference Manual, from IEEE Std 1003.1, 2004 Edition, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2004 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between these versions and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html. -.\" This notice shall appear on any product containing this material. -.\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License"). You may not use this file except in compliance with the License. -.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing. See the License for the specific language governing permissions and limitations under the License. -.\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE. If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner] -.TH REGCOMP 3C "Nov 1, 2003" -.SH NAME -regcomp, regexec, regerror, regfree \- regular expression matching -.SH SYNOPSIS -.LP -.nf -#include -#include - -\fBint\fR \fBregcomp\fR(\fBregex_t *restrict\fR \fIpreg\fR, \fBconst char *restrict\fR \fIpattern\fR, - \fBint\fR \fIcflags\fR); -.fi - -.LP -.nf -\fBint\fR \fBregexec\fR(\fBconst regex_t *restrict\fR \fIpreg\fR, - \fBconst char *restrict\fR \fIstring\fR, \fBsize_t\fR \fInmatch\fR, - \fBregmatch_t\fR \fIpmatch\fR[restrict], \fBint\fR \fIeflags\fR); -.fi - -.LP -.nf -\fBsize_t\fR \fBregerror\fR(\fBint\fR \fIerrcode\fR, \fBconst regex_t *restrict\fR \fIpreg\fR, - \fBchar *restrict\fR \fIerrbuf\fR, \fBsize_t\fR \fIerrbuf_size\fR); -.fi - -.LP -.nf -\fBvoid\fR \fBregfree\fR(\fBregex_t *\fR\fIpreg\fR); -.fi - -.SH DESCRIPTION -.sp -.LP -These functions interpret \fIbasic\fR and \fIextended\fR regular expressions -(described on the \fBregex\fR(5) manual page). -.sp -.LP -The structure type \fBregex_t\fR contains at least the following member: -.sp -.ne 2 -.na -\fB\fBsize_t\fR \fBre_nsub\fR\fR -.ad -.RS 18n -Number of parenthesised subexpressions. -.RE - -.sp -.LP -The structure type \fBregmatch_t\fR contains at least the following members: -.sp -.ne 2 -.na -\fB\fBregoff_t\fR \fBrm_so\fR\fR -.ad -.RS 18n -Byte offset from start of \fIstring\fR to start of substring. -.RE - -.sp -.ne 2 -.na -\fB\fBregoff_t\fR \fBrm_eo\fR\fR -.ad -.RS 18n -Byte offset from start of \fIstring\fR of the first character after the end of -substring. -.RE - -.SS "\fBregcomp()\fR" -.sp -.LP -The \fBregcomp()\fR function will compile the regular expression contained in -the string pointed to by the \fIpattern\fR argument and place the results in -the structure pointed to by \fIpreg\fR. The \fIcflags\fR argument is the -bitwise inclusive \fBOR\fR of zero or more of the following flags, which are -defined in the header \fB\fR: -.sp -.ne 2 -.na -\fB\fBREG_EXTENDED\fR\fR -.ad -.RS 16n -Use Extended Regular Expressions. -.RE - -.sp -.ne 2 -.na -\fB\fBREG_ICASE\fR\fR -.ad -.RS 16n -Ignore case in match. -.RE - -.sp -.ne 2 -.na -\fB\fBREG_NOSUB\fR\fR -.ad -.RS 16n -Report only success/fail in \fBregexec()\fR. -.RE - -.sp -.ne 2 -.na -\fB\fBREG_NEWLINE\fR\fR -.ad -.RS 16n -Change the handling of \fBNEWLINE\fR characters, as described in the text. -.RE - -.sp -.LP -The default regular expression type for \fIpattern\fR is a Basic Regular -Expression. The application can specify Extended Regular Expressions using the -\fBREG_EXTENDED\fR \fIcflags\fR flag. -.sp -.LP -If the \fBREG_NOSUB\fR flag was not set in \fIcflags\fR, then \fBregcomp()\fR -will set \fIre_nsub\fR to the number of parenthesised subexpressions (delimited -by \e(\e) in basic regular expressions or () in extended regular expressions) -found in \fI pattern\fR. -.SS "\fBregexec()\fR" -.sp -.LP -The \fBregexec()\fR function compares the null-terminated string specified by -\fIstring\fR with the compiled regular expression \fIpreg\fR initialized by a -previous call to \fBregcomp()\fR. The \fIeflags\fR argument is the bitwise -inclusive \fBOR\fR of zero or more of the following flags, which are defined in -the header <\fBregex.h\fR>: -.sp -.ne 2 -.na -\fB\fBREG_NOTBOL\fR\fR -.ad -.RS 14n -The first character of the string pointed to by \fIstring\fR is not the -beginning of the line. Therefore, the circumflex character (\fI^\fR), when -taken as a special character, will not match the beginning of \fIstring\fR. -.RE - -.sp -.ne 2 -.na -\fB\fBREG_NOTEOL\fR\fR -.ad -.RS 14n -The last character of the string pointed to by \fIstring\fR is not the end of -the line. Therefore, the dollar sign (\fI$\fR), when taken as a special -character, will not match the end of \fIstring\fR. -.RE - -.sp -.LP -If \fInmatch\fR is zero or \fBREG_NOSUB\fR was set in the \fIcflags\fR argument -to \fBregcomp()\fR, then \fBregexec()\fR will ignore the \fIpmatch\fR argument. -Otherwise, the \fIpmatch\fR argument must point to an array with at least -\fInmatch\fR elements, and \fBregexec()\fR will fill in the elements of that -array with offsets of the substrings of \fIstring\fR that correspond to the -parenthesised subexpressions of \fIpattern\fR: -\fIpmatch\fR\fB[\fR\fIi\fR\fB]\fR.\fIrm_so\fR will be the byte offset of the -beginning and \fIpmatch\fR\fB[\fR\fIi\fR\fB]\fR.\fIrm_eo\fR will be one greater -than the byte offset of the end of substring \fIi\fR. (Subexpression \fIi\fR -begins at the \fIi\fRth matched open parenthesis, counting from 1.) Offsets in -\fIpmatch\fR\fB[0]\fR identify the substring that corresponds to the entire -regular expression. Unused elements of \fIpmatch\fR up to -\fIpmatch\fR\fB[\fR\fInmatch\fR\fB\(mi1]\fR will be filled with \fB\(mi1\fR\&. -If there are more than \fInmatch\fR subexpressions in \fIpattern\fR -(\fIpattern\fR itself counts as a subexpression), then \fBregexec()\fR will -still do the match, but will record only the first \fInmatch\fR substrings. -.sp -.LP -When matching a basic or extended regular expression, any given parenthesised -subexpression of \fIpattern\fR might participate in the match of several -different substrings of \fIstring\fR, or it might not match any substring even -though the pattern as a whole did match. The following rules are used to -determine which substrings to report in \fIpmatch\fR when matching regular -expressions: -.sp -.ne 2 -.na -\fB1.\fR -.ad -.RS 6n -If subexpression \fIi\fR in a regular expression is not contained within -another subexpression, and it participated in the match several times, then the -byte offsets in \fIpmatch\fR\fB[\fR\fIi\fR\fB]\fR will delimit the last such -match. -.RE - -.sp -.ne 2 -.na -\fB2.\fR -.ad -.RS 6n -If subexpression \fIi\fR is not contained within another subexpression, and it -did not participate in an otherwise successful match, the byte offsets in -\fIpmatch\fR\fB[\fR\fIi\fR\fB]\fR will be \fB\(mi1\fR\&. A subexpression does -not participate in the match when: -.sp -\fB*\fR or \fB\e{\e} \fR appears immediately after the subexpression in a basic -regular expression, or \fB*\fR, \fB?\fR, or \fB{\|}\fR appears immediately -after the subexpression in an extended regular expression, and the -subexpression did not match (matched zero times) -.sp -or -.sp -\fB|\fR is used in an extended regular expression to select this subexpression -or another, and the other subexpression matched. -.RE - -.sp -.ne 2 -.na -\fB3.\fR -.ad -.RS 6n -If subexpression \fIi\fR is contained within another subexpression \fIj\fR, and -\fIi\fR is not contained within any other subexpression that is contained -within \fIj\fR, and a match of subexpression \fIj\fR is reported in -\fIpmatch\fR\fB[\fR\fIj\fR\fB]\fR, then the match or non-match of subexpression -\fIi\fR reported in \fIpmatch\fR\fB[\fR\fIi\fR\fB]\fR will be as described in -1. and 2. above, but within the substring reported in -\fIpmatch\fR\fB[\fR\fIj\fR\fB]\fR rather than the whole string. -.RE - -.sp -.ne 2 -.na -\fB4.\fR -.ad -.RS 6n -If subexpression \fIi\fR is contained in subexpression \fIj\fR, and the byte -offsets in \fIpmatch\fR\fB[\fR\fIj\fR\fB]\fR are \fB\(mi1\fR, then the pointers -in \fIpmatch\fR\fB[\fR\fIi\fR\fB]\fR also will be \fB\(mi1\fR\&. -.RE - -.sp -.ne 2 -.na -\fB5.\fR -.ad -.RS 6n -If subexpression \fIi\fR matched a zero-length string, then both byte offsets -in \fIpmatch\fR\fB[\fR\fIi\fR\fB]\fR will be the byte offset of the character -or \fINULL\fR terminator immediately following the zero-length string. -.RE - -.sp -.LP -If, when \fBregexec()\fR is called, the locale is different from when the -regular expression was compiled, the result is undefined. -.sp -.LP -If \fBREG_NEWLINE\fR is not set in \fIcflags\fR, then a \fBNEWLINE\fR character -in \fIpattern\fR or \fIstring\fR will be treated as an ordinary character. If -\fBREG_NEWLINE\fR is set, then newline will be treated as an ordinary character -except as follows: -.sp -.ne 2 -.na -\fB1.\fR -.ad -.RS 6n -A \fBNEWLINE\fR character in \fIstring\fR will not be matched by a period -outside a bracket expression or by any form of a non-matching list. -.RE - -.sp -.ne 2 -.na -\fB2.\fR -.ad -.RS 6n -A circumflex (^) in \fIpattern\fR, when used to specify expression anchoring -will match the zero-length string immediately after a newline in \fIstring\fR, -regardless of the setting of \fBREG_NOTBOL\fR. -.RE - -.sp -.ne 2 -.na -\fB3.\fR -.ad -.RS 6n -A dollar-sign ($) in \fIpattern\fR, when used to specify expression anchoring, -will match the zero-length string immediately before a newline in \fIstring\fR, -regardless of the setting of \fBREG_NOTEOL.\fR -.RE - -.SS "\fBregfree()\fR" -.sp -.LP -The \fBregfree()\fR function frees any memory allocated by \fBregcomp()\fR -associated with \fIpreg\fR. -.sp -.LP -The following constants are defined as error return values: -.sp -.ne 2 -.na -\fB\fBREG_NOMATCH\fR\fR -.ad -.RS 16n -The \fBregexec()\fR function failed to match. -.RE - -.sp -.ne 2 -.na -\fB\fBREG_BADPAT\fR\fR -.ad -.RS 16n -Invalid regular expression. -.RE - -.sp -.ne 2 -.na -\fB\fBREG_ECOLLATE\fR\fR -.ad -.RS 16n -Invalid collating element referenced. -.RE - -.sp -.ne 2 -.na -\fB\fBREG_ECTYPE\fR\fR -.ad -.RS 16n -Invalid character class type referenced. -.RE - -.sp -.ne 2 -.na -\fB\fBREG_EESCAPE\fR\fR -.ad -.RS 16n -Trailing \e in pattern. -.RE - -.sp -.ne 2 -.na -\fB\fBREG_ESUBREG\fR\fR -.ad -.RS 16n -Number in \e\fIdigit\fR invalid or in error. -.RE - -.sp -.ne 2 -.na -\fB\fBREG_EBRACK\fR\fR -.ad -.RS 16n -\fB[\|]\fR imbalance. -.RE - -.sp -.ne 2 -.na -\fB\fBREG_ENOSYS\fR\fR -.ad -.RS 16n -The function is not supported. -.RE - -.sp -.ne 2 -.na -\fB\fBREG_EPAREN\fR\fR -.ad -.RS 16n -\fB\e(\|\e)\fR or \fB()\fR imbalance. -.RE - -.sp -.ne 2 -.na -\fB\fBREG_EBRACE\fR\fR -.ad -.RS 16n -\e{ \e} imbalance. -.RE - -.sp -.ne 2 -.na -\fB\fBREG_BADBR\fR\fR -.ad -.RS 16n -Content of \e{ \e} invalid: not a number, number too large, more than two -numbers, first larger than second. -.RE - -.sp -.ne 2 -.na -\fB\fBREG_ERANGE\fR\fR -.ad -.RS 16n -Invalid endpoint in range expression. -.RE - -.sp -.ne 2 -.na -\fB\fBREG_ESPACE\fR\fR -.ad -.RS 16n -Out of memory. -.RE - -.sp -.ne 2 -.na -\fB\fBREG_BADRPT\fR\fR -.ad -.RS 16n -?, * or + not preceded by valid regular expression. -.RE - -.SS "\fBregerror()\fR" -.sp -.LP -The \fBregerror()\fR function provides a mapping from error codes returned by -\fBregcomp()\fR and \fBregexec()\fR to unspecified printable strings. It -generates a string corresponding to the value of the \fIerrcode\fR argument, -which must be the last non-zero value returned by \fBregcomp()\fR or -\fBregexec()\fR with the given value of \fIpreg\fR. If \fIerrcode\fR is not -such a value, an error message indicating that the error code is invalid is -returned. -.sp -.LP -If \fIpreg\fR is a \fINULL\fR pointer, but \fIerrcode\fR is a value returned by -a previous call to \fBregexec()\fR or \fBregcomp()\fR, the \fBregerror()\fR -still generates an error string corresponding to the value of \fIerrcode\fR. -.sp -.LP -If the \fIerrbuf_size\fR argument is not zero, \fBregerror()\fR will place the -generated string into the buffer of size \fIerrbuf_size\fR bytes pointed to by -\fIerrbuf\fR. If the string (including the terminating \fBNULL)\fR cannot fit -in the buffer, \fBregerror()\fR will truncate the string and null-terminate the -result. -.sp -.LP -If \fIerrbuf_size\fR is zero, \fBregerror()\fR ignores the \fIerrbuf\fR -argument, and returns the size of the buffer needed to hold the generated -string. -.sp -.LP -If the \fIpreg\fR argument to \fBregexec()\fR or \fBregfree()\fR is not a -compiled regular expression returned by \fBregcomp()\fR, the result is -undefined. A \fIpreg\fR is no longer treated as a compiled regular expression -after it is given to \fBregfree()\fR. -.sp -.LP -See \fBregex\fR(5) for BRE (Basic Regular Expression) Anchoring. -.SH RETURN VALUES -.sp -.LP -On successful completion, the \fBregcomp()\fR function returns \fB0\fR. -Otherwise, it returns an integer value indicating an error as described in -<\fBregex.h\fR>, and the content of \fIpreg\fR is undefined. -.sp -.LP -On successful completion, the \fBregexec()\fR function returns \fB0\fR. -Otherwise it returns \fBREG_NOMATCH\fR to indicate no match, or -\fBREG_ENOSYS\fR to indicate that the function is not supported. -.sp -.LP -Upon successful completion, the \fBregerror()\fR function returns the number of -bytes needed to hold the entire generated string. Otherwise, it returns \fB0\fR -to indicate that the function is not implemented. -.sp -.LP -The \fBregfree()\fR function returns no value. -.SH ERRORS -.sp -.LP -No errors are defined. -.SH USAGE -.sp -.LP -An application could use: -.sp -.LP -\fBregerror(code,preg,(char *)NULL,(size_t)0)\fR -.sp -.LP -to find out how big a buffer is needed for the generated string, \fBmalloc\fR a -buffer to hold the string, and then call \fBregerror()\fR again to get the -string (see \fBmalloc\fR(3C)). Alternately, it could allocate a fixed, static -buffer that is big enough to hold most strings, and then use \fBmalloc()\fR to -allocate a larger buffer if it finds that this is too small. -.SH EXAMPLES -.LP -\fBExample 1 \fRExample to match string against the extended regular expression -in pattern. -.sp -.in +2 -.nf -#include -/* -* Match string against the extended regular expression in -* pattern, treating errors as no match. -* -* return 1 for match, 0 for no match -*/ - -int -match(const char *string, char *pattern) -{ - int status; - regex_t re; - if (regcomp(&re, pattern, REG_EXTENDED\||\|REG_NOSUB) != 0) { - return(0); /* report error */ - } - status = regexec(&re, string, (size_t) 0, NULL, 0); - regfree(&re); - if (status != 0) { - return(0); /* report error */ - } - return(1); -} -.fi -.in -2 - -.sp -.LP -The following demonstrates how the \fBREG_NOTBOL\fR flag could be used with -\fBregexec()\fR to find all substrings in a line that match a pattern supplied -by a user. (For simplicity of the example, very little error checking is done.) - -.sp -.in +2 -.nf -(void) regcomp (&re, pattern, 0); -/* this call to regexec(\|) finds the first match on the line */ -error = regexec (&re, &buffer[0], 1, &pm, 0); -while (error == 0) { /* while matches found */ - /* substring found between pm.rm_so and pm.rm_eo */ - /* This call to regexec(\|) finds the next match */ - error = regexec (&re, buffer + pm.rm_eo, 1, &pm, REG_NOTBOL); -} -.fi -.in -2 - -.SH ATTRIBUTES -.sp -.LP -See \fBattributes\fR(5) for descriptions of the following attributes: -.sp - -.sp -.TS -box; -c | c -l | l . -ATTRIBUTE TYPE ATTRIBUTE VALUE -_ -CSI Enabled -_ -Interface Stability Standard -_ -MT-Level MT-Safe with exceptions -.TE - -.SH SEE ALSO -.sp -.LP -\fBfnmatch\fR(3C), \fBglob\fR(3C), \fBmalloc\fR(3C), \fBsetlocale\fR(3C), -\fBattributes\fR(5), \fBstandards\fR(5), \fBregex\fR(5) -.SH NOTES -.sp -.LP -The \fBregcomp()\fR function can be used safely in a multithreaded application -as long as \fBsetlocale\fR(3C) is not being called to change the locale. +.\" Copyright (c) 1992, 1993, 1994 Henry Spencer. +.\" Copyright (c) 1992, 1993, 1994 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" This code is derived from software contributed to Berkeley by +.\" Henry Spencer. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" 3. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.\" +.\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for permission +.\" to reproduce portions of its copyrighted documentation. +.\" +.\" Original documentation from The Open Group can be obtained online at +.\" http://www.opengroup.org/bookstore/. +.\" +.\" The Institute of Electrical and Electronics Engineers and The Open Group, +.\" have given us permission to reprint portions of their documentation. In the +.\" following statement, the phrase "this text" refers to portions of the system +.\" documentation. +.\" +.\" Portions of this text are reprinted and reproduced in electronic form in the +.\" Sun OS Reference Manual, from IEEE Std 1003.1, 2004 Edition, Standard for +.\" Information Technology -- Portable Operating System Interface (POSIX), +.\" The Open Group Base Specifications Issue 6, Copyright (C) 2001-2004 by the +.\" Institute of Electrical and Electronics Engineers, Inc and The Open Group. +.\" +.\" In the event of any discrepancy between these versions and the original +.\" IEEE and The Open Group Standard, the original IEEE and The Open Group +.\" Standard is the referee document. +.\" +.\" The original Standard can be obtained online at +.\" http://www.opengroup.org/unix/online.html. +.\" +.\" This notice shall appear on any product containing this material. +.\" +.\" The contents of this file are subject to the terms of the +.\" Common Development and Distribution License (the "License"). +.\" You may not use this file except in compliance with the License. +.\" +.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE +.\" or http://www.opensolaris.org/os/licensing. +.\" See the License for the specific language governing permissions +.\" and limitations under the License. +.\" +.\" When distributing Covered Code, include this CDDL HEADER in each +.\" file and include the License file at usr/src/OPENSOLARIS.LICENSE. +.\" If applicable, add the following below this CDDL HEADER, with the +.\" fields enclosed by brackets "[]" replaced with your own identifying +.\" information: Portions Copyright [yyyy] [name of copyright owner] +.\" +.\" +.\" Copyright (c) 1992, X/Open Company Limited. All Rights Reserved. +.\" Portions Copyright (c) 2003, Sun Microsystems, Inc. All Rights Reserved. +.\" Copyright 2017 Nexenta Systems, Inc. +.\" +.Dd June 14, 2017 +.Dt REGCOMP 3C +.Os +.Sh NAME +.Nm regcomp , +.Nm regexec , +.Nm regerror , +.Nm regfree +.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" +.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" +.Fc +.Ft size_t +.Fo regerror +.Fa "int errcode" "const regex_t *restrict preg" +.Fa "char *restrict errbuf" "size_t errbuf_size" +.Fc +.Ft void +.Fn regfree "regex_t *preg" +.Sh DESCRIPTION +These routines implement +.St -p1003.2 +regular expressions; see +.Xr regex 5 . +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. +.Pp +The header +.In 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 +.Qq Dv REG_ . +.Ss Fn regcomp +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 OR of zero or more of the following flags: +.Bl -tag -width REG_EXTENDED +.It Dv REG_EXTENDED +Compile extended regular expressions +.Pq EREs , +rather than the basic regular expressions +.Pq BREs +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, so the 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 . +.It Dv REG_ICASE +Compile for matching that ignores upper/lower case distinctions. +See +.Xr regex 5 . +.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, +.Qq [^ +bracket expressions and +.Qq \&. +never match newline, +a +.Qq \&^ +anchor matches the null string after any newline in the string in addition to +its normal function, and the +.Qq \&$ +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, but just before the character +pointed to by the +.Va re_endp +member of the structure pointed to by +.Fa preg . +The +.Va re_endp +member is of type +.Vt "const char *" . +This flag permits inclusion of NULs 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. +.El +.Pp +When successful, +.Fn regcomp +returns 0 and fills in the structure pointed to by +.Fa preg . +One member of that structure +.Po other than +.Va re_endp +.Pc +is publicized: +.Va re_nsub , +of type +.Ft size_t , +contains the number of parenthesized subexpressions within the RE +.Po except that the value of this member is undefined if the +.Dv REG_NOSUB +flag was used +.Pc . +.Ss Fn regexec +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. +.Pp +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 OR of zero or more of the following flags: +.Bl -tag -width REG_STARTEND +.It Dv REG_NOTBOL +The first character of the string is treated as the continuation +of a line. +This means that the anchors +.Qq \&^ , +.Qq [[:<:]] , +and +.Qq \e< +do not match before it; but see +.Dv REG_STARTEND +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 +.Qq \&$ +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 +and to end before the byte located at +.Fa string No + +.Fa pmatch Ns [0]. Ns Fa rm_eo , +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. +.Pp +Without +.Dv REG_NOTBOL , +the position +.Fa rm_so +is considered the beginning of a line, such that +.Qq \&^ +matches before it, and the beginning of a word if there is a word character at +this position, such that +.Qq [[:<:]] +and +.Qq \e< +match before it. +.Pp +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 , +.Qq ^ +matches before the string; if the preceding character is not a word character +but the string starts with a word character, +.Qq [[:<:]] +and +.Qq \e< +match before the string. +.El +.Pp +See +.Xr regex 5 +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 . +.Pp +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 +.Po but see below for the case where +.Dv REG_STARTEND +is specified +.Pc . +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 +.Po a signed arithmetic type at least as large as an +.Ft off_t +and a +.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 +.Fa string +argument given to +.Fn regexec . +An empty substring is denoted by equal offsets, both indicating the character +following the empty substring. +.Pp +The 0th 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 +.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 +.Po that is, +.Va i +> +.Fa preg Ns -> Ns Va re_nsub +.Pc +.Pc +have both +.Va rm_so +and +.Va rm_eo +set to -1. +If a subexpression participated in the match several times, +the reported substring is the last one it matched. +.Po Note, as an example in particular, that when the RE +.Qq (b*)+ +matches +.Qq 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 +.Qq b , +so the reported substring is one of the empties. +.Pc +.Pp +If +.Dv REG_STARTEND +is specified, +.Fa pmatch +must point to at least one +.Ft regmatch_t +.Po even if +.Fa nmatch +is 0 or +.Dv REG_NOSUB +was specified +.Pc , +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 [0] +will not be changed by a successful +.Fn regexec . +.Ss Fn regerror +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 non-NULL, the error code should have arisen from use of the +.Ft regex_t +pointed to by +.Fa preg , +and if the error code came from +.Fn regcomp , +it should have been the result from the most recent +.Fn regcomp +using that +.Ft regex_t . +The +.Po +.Fn regerror +may be able to supply a more detailed message using information +from the +.Ft regex_t . +.Pc +The +.Fn regerror +function places the NUL-terminated message into the buffer pointed to by +.Fa errbuf , +limiting the length +.Pq including the 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 +.Pq including terminating NUL . +If +.Fa errbuf_size +is 0, +.Fa errbuf +is ignored but the return value is still correct. +.Pp +If the +.Fa errcode +given to +.Fn regerror +is first ORed with +.Dv REG_ITOA , +the +.Qq message +that results is the printable name of the error code, e.g. +.Qq Dv REG_NOMATCH , +rather than an explanation thereof. +If +.Fa errcode +is +.Dv REG_ATOI , +then +.Fa preg +shall be non-NULL and the +.Va 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 +.Pq 0 if the name is not recognized . +.Dv REG_ITOA +and +.Dv REG_ATOI +are intended primarily as debugging facilities; they are extensions, +compatible with but not specified by +.St -p1003.2 , +and should be used with caution in software intended to be portable to other +systems. +.Ss Fn regfree +The +.Fn regfree +function frees any dynamically-allocated storage associated with the compiled RE +pointed to by +.Fa preg . +The remaining +.Ft regex_t +is no longer a valid compiled RE and the effect of supplying it to +.Fn regexec +or +.Fn regerror +is undefined. +.Sh IMPLEMENTATION NOTES +There are a number of decisions that +.St -p1003.2 +leaves up to the implementor, +either by explicitly saying +.Qq undefined +or by virtue of them being forbidden by the RE grammar. +This implementation treats them as follows. +.Pp +There is no particular limit on the length of REs, except insofar as memory is +limited. +Memory usage is approximately linear in RE size, and largely insensitive +to RE complexity, except for bounded repetitions. +.Pp +A backslashed character other than one specifically given a magic meaning by +.St -p1003.2 +.Pq such magic meanings occur only in BREs +is taken as an ordinary character. +.Pp +Any unmatched +.Qq \&[ +is a +.Dv REG_EBRACK +error. +.Pp +Equivalence classes cannot begin or end bracket-expression ranges. +The endpoint of one range cannot begin another. +.Pp +.Dv RE_DUP_MAX , +the limit on repetition counts in bounded repetitions, is 255. +.Pp +A repetition operator +.Po +.Qq \&? , +.Qq \&* , +.Qq \&+ , +or bounds +.Pc +cannot follow another repetition operator. +A repetition operator cannot begin an expression or subexpression +or follow +.Qq \&^ +or +.Qq \&| . +.Pp +.Qq \&| +cannot appear first or last in a (sub)expression or after another +.Qq \&| , +i.e., an operand of +.Qq \&| +cannot be an empty subexpression. +An empty parenthesized subexpression, +.Qq () , +is legal and matches an empty (sub)string. +An empty string is not a legal RE. +.Pp +A +.Qq \&{ +followed by a digit is considered the beginning of bounds for a bounded +repetition, which must then follow the syntax for bounds. +A +.Qq \&{ +.Em not +followed by a digit is considered an ordinary character. +.Pp +.Qq \&^ +and +.Qq \&$ +beginning and ending subexpressions in BREs are anchors, not ordinary +characters. +.Sh RETURN VALUES +On successful completion, the +.Fn regcomp +function returns 0. +Otherwise, it returns an integer value indicating an error as described in +.In regex.h , +and the content of preg is undefined. +.Pp +On successful completion, the +.Fn regexec +function returns 0. +Otherwise it returns +.Dv REG_NOMATCH +to indicate no match, or +.Dv REG_ENOSYS +to indicate that the function is not supported. +.Pp +Upon successful completion, the +.Fn regerror +function returns the number of bytes needed to hold the entire generated string. +Otherwise, it returns 0 to indicate that the function is not implemented. +.Pp +The +.Fn regfree +function returns no value. +.Pp +The following constants are defined as error return values: +.Pp +.Bl -tag -width "REG_ECOLLATE" -compact +.It Dv REG_NOMATCH +The +.Fn regexec +function failed to match. +.It Dv REG_BADPAT +Invalid regular expression. +.It Dv REG_ECOLLATE +Invalid collating element referenced. +.It Dv REG_ECTYPE +Invalid character class type referenced. +.It Dv REG_EESCAPE +Trailing +.Qq \&\e +in pattern. +.It Dv REG_ESUBREG +Number in +.Qq \&\e Ns Em digit +invalid or in error. +.It Dv REG_EBRACK +.Qq [] +imbalance. +.It Dv REG_ENOSYS +The function is not supported. +.It Dv REG_EPAREN +.Qq \e(\e) +or +.Qq () +imbalance. +.It Dv REG_EBRACE +.Qq \e{\e} +imbalance. +.It Dv REG_BADBR +Content of +.Qq \e{\e} +invalid: not a number, number too large, more than two +numbers, first larger than second. +.It Dv REG_ERANGE +Invalid endpoint in range expression. +.It Dv REG_ESPACE +Out of memory. +.It Dv REG_BADRPT +.Qq \&? , +.Qq * +or +.Qq + +not preceded by valid regular expression. +.El +.Sh USAGE +An application could use: +.Bd -literal -offset Ds +regerror(code, preg, (char *)NULL, (size_t)0) +.Ed +.Pp +to find out how big a buffer is needed for the generated string, +.Fn malloc +a buffer to hold the string, and then call +.Fn regerror +again to get the string +.Po see +.Xr malloc 3C +.Pc . +Alternately, it could allocate a fixed, static buffer that is big enough to hold +most strings, and then use +.Fn malloc +allocate a larger buffer if it finds that this is too small. +.Sh EXAMPLES +Matching string against the extended regular expression in pattern. +.Bd -literal -offset Ds +#include + +/* +* Match string against the extended regular expression in +* pattern, treating errors as no match. +* +* return 1 for match, 0 for no match +*/ +int +match(const char *string, char *pattern) +{ + int status; + regex_t re; + + if (regcomp(&re, pattern, REG_EXTENDED\||\|REG_NOSUB) != 0) { + return(0); /* report error */ + } + status = regexec(&re, string, (size_t) 0, NULL, 0); + regfree(&re); + if (status != 0) { + return(0); /* report error */ + } + return(1); +} +.Ed +.Pp +The following demonstrates how the +.Dv REG_NOTBOL +flag could be used with +.Fn regexec +to find all substrings in a line that match a pattern supplied by a user. +.Pq For simplicity of the example, very little error checking is done. +.Bd -literal -offset Ds +(void) regcomp(&re, pattern, 0); +/* this call to regexec() finds the first match on the line */ +error = regexec(&re, &buffer[0], 1, &pm, 0); +while (error == 0) { /* while matches found */ + /* substring found between pm.rm_so and pm.rm_eo */ + /* This call to regexec() finds the next match */ + error = regexec(&re, buffer + pm.rm_eo, 1, &pm, REG_NOTBOL); +} +.Ed +.Sh ERRORS +No errors are defined. +.Sh CODE SET INDEPENDENCE +.Sy Enabled +.Sh INTERFACE STABILITY +.Sy Standard +.Sh MT-LEVEL +.Sy MT-Safe with exceptions +.Pp +The +.Fn regcomp +function can be used safely in a multithreaded application as long as +.Xr setlocale 3C +is not being called to change the locale. +.Sh SEE ALSO +.Xr attributes 5 , +.Xr regex 5 , +.Xr standards 5 +.Pp +.St -p1003.2 , +sections 2.8 +.Pq Regular Expression Notation +and +B.5 +.Pq C Binding for Regular Expression Matching . diff --git a/usr/src/man/man5/regex.5 b/usr/src/man/man5/regex.5 dissimilarity index 84% index 68ae1656a7..8e267ac4c0 100644 --- a/usr/src/man/man5/regex.5 +++ b/usr/src/man/man5/regex.5 @@ -1,769 +1,1041 @@ -'\" te -.\" Copyright (c) 1992, X/Open Company Limited All Rights Reserved -.\" Portions Copyright (c) 1999, Sun Microsystems, Inc. All Rights Reserved -.\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for permission to reproduce portions of its copyrighted documentation. Original documentation from The Open Group can be obtained online at -.\" http://www.opengroup.org/bookstore/. -.\" The Institute of Electrical and Electronics Engineers and The Open Group, have given us permission to reprint portions of their documentation. In the following statement, the phrase "this text" refers to portions of the system documentation. Portions of this text are reprinted and reproduced in electronic form in the Sun OS Reference Manual, from IEEE Std 1003.1, 2004 Edition, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2004 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between these versions and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html. -.\" This notice shall appear on any product containing this material. -.\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License"). You may not use this file except in compliance with the License. -.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing. See the License for the specific language governing permissions and limitations under the License. -.\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE. If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner] -.TH REGEX 5 "Apr 21, 2005" -.SH NAME -regex \- internationalized basic and extended regular expression matching -.SH DESCRIPTION -.LP -Regular Expressions (REs) provide a mechanism to select specific strings from a -set of character strings. The Internationalized Regular Expressions described -below differ from the Simple Regular Expressions described on the -\fBregexp\fR(5) manual page in the following ways: -.RS +4 -.TP -.ie t \(bu -.el o -both Basic and Extended Regular Expressions are supported -.RE -.RS +4 -.TP -.ie t \(bu -.el o -the Internationalization features\(emcharacter class, equivalence class, and -multi-character collation\(emare supported. -.RE -.sp -.LP -The Basic Regular Expression (BRE) notation and construction rules described in -the \fBBASIC\fR \fBREGULAR\fR \fBEXPRESSIONS\fR section apply to most utilities -supporting regular expressions. Some utilities, instead, support the Extended -Regular Expressions (ERE) described in the \fBEXTENDED\fR \fBREGULAR\fR -\fBEXPRESSIONS\fR section; any exceptions for both cases are noted in the -descriptions of the specific utilities using regular expressions. Both BREs and -EREs are supported by the Regular Expression Matching interfaces -\fBregcomp\fR(3C) and \fBregexec\fR(3C). -.SH BASIC REGULAR EXPRESSIONS -.SS "BREs Matching a Single Character" -.LP -A BRE ordinary character, a special character preceded by a backslash, or a -period matches a single character. A bracket expression matches a single -character or a single collating element. See \fBRE Bracket Expression\fR, -below. -.SS "BRE Ordinary Characters" -.LP -An ordinary character is a BRE that matches itself: any character in the -supported character set, except for the BRE special characters listed in \fBBRE -Special Characters\fR, below. -.sp -.LP -The interpretation of an ordinary character preceded by a backslash (\e) is -undefined, except for: -.RS +4 -.TP -1. -the characters ), (, {, and } -.RE -.RS +4 -.TP -2. -the digits 1 to 9 inclusive (see \fBBREs Matching Multiple Characters\fR, -below) -.RE -.RS +4 -.TP -3. -a character inside a bracket expression. -.RE -.SS "BRE Special Characters" -.LP -A BRE \fIspecial\fR \fIcharacter\fR has special properties in certain contexts. -Outside those contexts, or when preceded by a backslash, such a character will -be a BRE that matches the special character itself. The BRE special characters -and the contexts in which they have their special meaning are: -.sp -.ne 2 -.na -\fB\&. [ \e\fR -.ad -.RS 12n -The period, left-bracket, and backslash are special except when used in a -bracket expression (see \fBRE Bracket Expression\fR, below). An expression -containing a [ that is not preceded by a backslash and is not part of a bracket -expression produces undefined results. -.RE - -.sp -.ne 2 -.na -\fB*\fR -.ad -.RS 12n -The asterisk is special except when used: -.RS +4 -.TP -.ie t \(bu -.el o -in a bracket expression -.RE -.RS +4 -.TP -.ie t \(bu -.el o -as the first character of an entire BRE (after an initial ^, if any) -.RE -.RS +4 -.TP -.ie t \(bu -.el o -as the first character of a subexpression (after an initial ^, if any); see -\fBBREs Matching Multiple Characters\fR, below. -.RE -.RE - -.sp -.ne 2 -.na -\fB\fI^\fR\fR -.ad -.RS 12n -The circumflex is special when used: -.RS +4 -.TP -.ie t \(bu -.el o -as an anchor (see \fBBRE Expression Anchoring\fR, below). -.RE -.RS +4 -.TP -.ie t \(bu -.el o -as the first character of a bracket expression (see \fBRE Bracket -Expression\fR, below). -.RE -.RE - -.sp -.ne 2 -.na -\fB$\fR -.ad -.RS 12n -The dollar sign is special when used as an anchor. -.RE - -.SS "Periods in BREs" -.LP -A period (\fB\&.\fR), when used outside a bracket expression, is a BRE that -matches any character in the supported character set except NUL. -.SS "RE Bracket Expression" -.LP -A bracket expression (an expression enclosed in square brackets, [\|]) is an RE -that matches a single collating element contained in the non-empty set of -collating elements represented by the bracket expression. -.sp -.LP -The following rules and definitions apply to bracket expressions: -.RS +4 -.TP -1. -A \fIbracket expression\fR is either a matching list expression or a -non-matching list expression. It consists of one or more expressions: collating -elements, collating symbols, equivalence classes, character classes, or range -expressions (see rule 7 below). Portable applications must not use range -expressions, even though all implementations support them. The right-bracket -(]) loses its special meaning and represents itself in a bracket expression if -it occurs first in the list (after an initial circumflex (^), if any). -Otherwise, it terminates the bracket expression, unless it appears in a -collating symbol (such as [.].]) or is the ending right-bracket for a collating -symbol, equivalence class, or character class. The special characters: -.sp -.in +2 -.nf - . * [ \e -.fi -.in -2 -.sp - -(period, asterisk, left-bracket and backslash, respectively) lose their special -meaning within a bracket expression. -.sp -The character sequences: -.sp -.in +2 -.nf - [. [= [: -.fi -.in -2 -.sp - -(left-bracket followed by a period, equals-sign, or colon) are special inside a -bracket expression and are used to delimit collating symbols, equivalence class -expressions, and character class expressions. These symbols must be followed by -a valid expression and the matching terminating sequence .], =] or :], as -described in the following items. -.RE -.RS +4 -.TP -2. -A \fImatching list\fR expression specifies a list that matches any one of -the expressions represented in the list. The first character in the list must -not be the circumflex. For example, \fB[abc]\fR is an RE that matches any of -the characters \fBa\fR, \fBb\fR or \fBc\fR. -.RE -.RS +4 -.TP -3. -A \fInon-matching list\fR expression begins with a circumflex (^), and -specifies a list that matches any character or collating element except for the -expressions represented in the list after the leading circumflex. For example, -[^abc] is an RE that matches any character or collating element except the -characters \fBa,\fR \fBb\fR, or \fBc\fR. The circumflex will have this special -meaning only when it occurs first in the list, immediately following the -left-bracket. -.RE -.RS +4 -.TP -4. -A \fIcollating symbol\fR is a collating element enclosed within -bracket-period ([..]) delimiters. Multi-character collating elements must be -represented as collating symbols when it is necessary to distinguish them from -a list of the individual characters that make up the multi-character collating -element. For example, if the string \fBch\fR is a collating element in the -current collation sequence with the associated collating symbol , the -expression [[.ch.]] will be treated as an RE matching the character sequence -\fBch\fR, while [ch] will be treated as an RE matching \fBc\fR or \fBh\fR. -Collating symbols will be recognized only inside bracket expressions. This -implies that the RE \fB[[.ch.]]*c\fR matches the first to fifth character in -the string chchch. If the string is not a collating element in the current -collating sequence definition, or if the collating element has no characters -associated with it, the symbol will be treated as an invalid expression. -.RE -.RS +4 -.TP -5. -An \fIequivalence class expression\fR represents the set of collating -elements belonging to an equivalence class. Only primary equivalence classes -will be recognised. The class is expressed by enclosing any one of the -collating elements in the equivalence class within bracket-equal ([==]) -delimiters. For example, if a and b belong to the same equivalence class, then -[[=a=]b], [[==]b] and [[==]b] will each be equivalent to [ab]. If the collating -element does not belong to an equivalence class, the equivalence class -expression will be treated as a \fIcollating symbol\fR. -.RE -.RS +4 -.TP -6. -A \fIcharacter class expression\fR represents the set of characters -belonging to a character class, as defined in the \fBLC_CTYPE\fR category in -the current locale. All character classes specified in the current locale will -be recognized. A character class expression is expressed as a character class -name enclosed within bracket-colon ([::]) delimiters. -.sp -The following character class expressions are supported in all locales: -.sp - -.sp -.TS -l l l l -l l l l . -\fB[:alnum:]\fR \fB[:cntrl:]\fR \fB[:lower:]\fR \fB[:space:]\fR -\fB[:alpha:]\fR \fB[:digit:]\fR \fB[:print:]\fR \fB[:upper:]\fR -\fB[:blank:]\fR \fB[:graph:]\fR \fB[:punct:]\fR \fB[:xdigit:]\fR -.TE - -In addition, character class expressions of the form: -.sp -.in +2 -.nf - \fB[:name:]\fR -.fi -.in -2 -.sp - -are recognized in those locales where the \fIname\fR keyword has been given a -\fBcharclass\fR definition in the \fBLC_CTYPE\fR category. -.RE -.RS +4 -.TP -7. -A \fIrange expression\fR represents the set of collating elements that fall -between two elements in the current collation sequence, inclusively. It is -expressed as the starting point and the ending point separated by a hyphen -(\fB-\fR). -.sp -Range expressions must not be used in portable applications because their -behavior is dependent on the collating sequence. Ranges will be treated -according to the current collating sequence, and include such characters that -fall within the range based on that collating sequence, regardless of character -values. This, however, means that the interpretation will differ depending on -collating sequence. If, for instance, one collating sequence defines as a -variant of \fBa\fR, while another defines it as a letter following \fBz\fR, -then the expression [-z] is valid in the first language and invalid in the -second. -.sp -In the following, all examples assume the collation sequence specified for the -POSIX locale, unless another collation sequence is specifically defined. -.sp -The starting range point and the ending range point must be a collating element -or collating symbol. An equivalence class expression used as a starting or -ending point of a range expression produces unspecified results. An equivalence -class can be used portably within a bracket expression, but only outside the -range. For example, the unspecified expression [[=e=]\(mif] should be given as -[[=e=]e\(mif]. The ending range point must collate equal to or higher than the -starting range point; otherwise, the expression will be treated as invalid. The -order used is the order in which the collating elements are specified in the -current collation definition. One-to-many mappings (see \fBlocale\fR(5)) will -not be performed. For example, assuming that the character eszet is placed in -the collation sequence after \fBr\fR and \fBs\fR, but before \fBt\fR, and that -it maps to the sequence \fBss\fR for collation purposes, then the expression -[r\(mis] matches only \fBr\fR and \fBs\fR, but the expression [s\(mit] matches -\fBs\fR, \fBbeta\fR, or \fBt\fR. -.sp -The interpretation of range expressions where the ending range point is also -the starting range point of a subsequent range expression (for instance -[a\(mim\(mio]) is undefined. -.sp -The hyphen character will be treated as itself if it occurs first (after an -initial ^, if any) or last in the list, or as an ending range point in a range -expression. As examples, the expressions [\(miac] and [ac\(mi] are equivalent -and match any of the characters \fBa\fR, \fBc\fR, or \fB\(mi;\fR [^\(miac] and -[^ac\(mi] are equivalent and match any characters except \fBa\fR, \fBc\fR, or -\(mi; the expression [%\(mi\|\(mi] matches any of the characters between % and -\(mi inclusive; the expression [\(mi\|\(mi@] matches any of the characters -between \(mi and @ inclusive; and the expression [a\(mi\|\(mi@] is invalid, -because the letter \fBa\fR follows the symbol \(mi in the POSIX locale. To use -a hyphen as the starting range point, it must either come first in the bracket -expression or be specified as a collating symbol, for example: -[][.\(mi.]\(mi0], which matches either a right bracket or any character or -collating element that collates between hyphen and 0, inclusive. -.sp -If a bracket expression must specify both \(mi and ], the ] must be placed -first (after the ^, if any) and the \(mi last within the bracket expression. -.RE -.sp -.LP -Note: Latin-1 characters such as \(ga or ^ are not printable in some locales, -for example, the \fBja\fR locale. -.SS "BREs Matching Multiple Characters" -.LP -The following rules can be used to construct BREs matching multiple characters -from BREs matching a single character: -.RS +4 -.TP -1. -The concatenation of BREs matches the concatenation of the strings matched -by each component of the BRE. -.RE -.RS +4 -.TP -2. -A \fIsubexpression\fR can be defined within a BRE by enclosing it between -the character pairs \e( and \e) . Such a subexpression matches whatever it -would have matched without the \e( and \e), except that anchoring within -subexpressions is optional behavior; see \fBBRE Expression Anchoring\fR, below. -Subexpressions can be arbitrarily nested. -.RE -.RS +4 -.TP -3. -The \fIback-reference\fR expression \e\fIn\fR matches the same (possibly -empty) string of characters as was matched by a subexpression enclosed between -\e( and \e) preceding the \e\fIn\fR. The character \fIn\fR must be a digit from -1 to 9 inclusive, \fIn\fRth subexpression (the one that begins with the -\fIn\fRth \e( and ends with the corresponding paired \e)\|). The expression is -invalid if less than \fIn\fR subexpressions precede the \e\fIn\fR. For example, -the expression \|^\e(.*\e)\e1$ matches a line consisting of two adjacent -appearances of the same string, and the expression \e(a\e)*\e1 fails to match -\fBa\fR. The limit of nine back-references to subexpressions in the RE is based -on the use of a single digit identifier. This does not imply that only nine -subexpressions are allowed in REs. The following is a valid BRE with ten -subexpressions: -.sp -.in +2 -.nf -\fB\e(\e(\e(ab\e)*c\e)*d\e)\e(ef\e)*\e(gh\e)\e{2\e}\e(ij\e)*\e(kl\e)*\e(mn\e)*\e(op\e)*\e(qr\e)*\fR -.fi -.in -2 -.sp - -.RE -.RS +4 -.TP -4. -When a BRE matching a single character, a subexpression or a back-reference -is followed by the special character asterisk (*), together with that asterisk -it matches what zero or more consecutive occurrences of the BRE would match. -For example, \fB[ab]*\fR \fBand\fR \fB[ab][ab]\fR are equivalent when matching -the string \fBab\fR. -.RE -.RS +4 -.TP -5. -When a BRE matching a single character, a subexpression, or a back-reference -is followed by an \fIinterval expression\fR of the format \e{\fIm\fR\e}, -\e{\fIm\fR,\e} or \e{\fIm\fR,\fIn\fR\e}, together with that interval expression -it matches what repeated consecutive occurrences of the BRE would match. The -values of \fIm\fR and \fIn\fR will be decimal integers in the range 0 \(<= -\fIm\fR \(<= \fIn\fR \(<= {\fBRE_DUP_MAX\fR}, where \fIm\fR specifies the exact -or minimum number of occurrences and \fIn\fR specifies the maximum number of -occurrences. The expression \e{\fIm\fR\e} matches exactly \fIm\fR occurrences -of the preceding BRE, \e{\fIm\fR,\e} matches at least \fIm\fR occurrences and -\e{\fIm,n\fR\e} matches any number of occurrences between \fIm\fR and \fIn\fR, -inclusive. -.sp -For example, in the string \fBabababccccccd\fR, the BRE c\e{3\e} is matched by -characters seven to nine, the BRE \e(ab\e)\e{4,\e} is not matched at all and -the BRE c\e{1,3\e}d is matched by characters ten to thirteen. -.RE -.sp -.LP -The behavior of multiple adjacent duplication symbols \fB(\fR \fB*\fR \fB\fR -and intervals) produces undefined results. -.SS "BRE Precedence" -.LP -The order of precedence is as shown in the following table: -.sp -.in +2 -.nf - -.fi -.in -2 -.sp - -.sp - -.sp -.TS -box; -l l -l l . -\fBBRE Precedence (from high to low)\fR -collation-related bracket symbols [= =] [: :] [. .] -escaped characters \e<\fIspecial character\fR> -bracket expression [ ] -subexpressions/back-references \e( \e) \e\fIn\fR -single-character-BRE duplication * \e{\fIm\fR,\fIn\fR\e} -concatenation -anchoring ^ $ -.TE - -.SS "BRE Expression Anchoring" -.LP -A BRE can be limited to matching strings that begin or end a line; this is -called \fIanchoring\fR. The circumflex and dollar sign special characters will -be considered BRE anchors in the following contexts: -.RS +4 -.TP -1. -A circumflex \fB(\fR \fB^\fR \fB)\fR is an anchor when used as the first -character of an entire BRE. The implementation may treat circumflex as an -anchor when used as the first character of a subexpression. The circumflex will -anchor the expression to the beginning of a string; only sequences starting at -the first character of a string will be matched by the BRE. For example, the -BRE ^ab matches \fBab\fR in the string \fBabcdef,\fR but fails to match in the -string \fBcdefab\fR. A portable BRE must escape a leading circumflex in a -subexpression to match a literal circumflex. -.RE -.RS +4 -.TP -2. -A dollar sign \fB(\fR \fB$\fR \fB)\fR is an anchor when used as the last -character of an entire BRE. The implementation may treat a dollar sign as an -anchor when used as the last character of a subexpression. The dollar sign will -anchor the expression to the end of the string being matched; the dollar sign -can be said to match the end-of-string following the last character. -.RE -.RS +4 -.TP -3. -A BRE anchored by both \fB^\fR and \fB$\fR matches only an entire string. -For example, the BRE \fB^abcdef$\fR matches strings consisting only of -\fBabcdef\fR. -.RE -.RS +4 -.TP -4. -\fB^\fR and \fB$\fR are not special in subexpressions. -.RE -.sp -.LP -Note: The Solaris implementation does not support anchoring in BRE -subexpressions. -.SH EXTENDED REGULAR EXPRESSIONS -.LP -The rules specified for BREs apply to Extended Regular Expressions (EREs) with -the following exceptions: -.RS +4 -.TP -.ie t \(bu -.el o -The characters \fB|\fR, \fB+\fR, and \fB?\fR have special meaning, as defined -below. -.RE -.RS +4 -.TP -.ie t \(bu -.el o -The \fB{\fR and \fB}\fR characters, when used as the duplication operator, are -not preceded by backslashes. The constructs \fB\e{\fR and \fB\e}\fR simply -match the characters \fB{\fR and \fB}\fR, respectively. -.RE -.RS +4 -.TP -.ie t \(bu -.el o -The back reference operator is not supported. -.RE -.RS +4 -.TP -.ie t \(bu -.el o -Anchoring (\fB^$\fR) is supported in subexpressions. -.RE -.SS "EREs Matching a Single Character" -.LP -An ERE ordinary character, a special character preceded by a backslash, or a -period matches a single character. A bracket expression matches a single -character or a single collating element. An \fIERE\fR \fImatching\fR \fIa\fR -\fIsingle\fR \fIcharacter\fR enclosed in parentheses matches the same as the -ERE without parentheses would have matched. -.SS "ERE Ordinary Characters" -.LP -An \fIordinary character\fR is an ERE that matches itself. An ordinary -character is any character in the supported character set, except for the ERE -special characters listed in \fBERE\fR \fBSpecial\fR \fBCharacters\fR below. -The interpretation of an ordinary character preceded by a backslash (\fB\e\fR) -is undefined. -.SS "ERE Special Characters" -.LP -An \fIERE\fR \fIspecial\fR \fIcharacter\fR has special properties in certain -contexts. Outside those contexts, or when preceded by a backslash, such a -character is an ERE that matches the special character itself. The extended -regular expression special characters and the contexts in which they have their -special meaning are: -.sp -.ne 2 -.na -\fB\&. [ \e (\fR -.ad -.RS 14n -The period, left-bracket, backslash, and left-parenthesis are special except -when used in a bracket expression (see \fBRE Bracket Expression\fR, above). -Outside a bracket expression, a left-parenthesis immediately followed by a -right-parenthesis produces undefined results. -.RE - -.sp -.ne 2 -.na -\fB)\fR -.ad -.RS 14n -The right-parenthesis is special when matched with a preceding -left-parenthesis, both outside a bracket expression. -.RE - -.sp -.ne 2 -.na -\fB* + ? {\fR -.ad -.RS 14n -The asterisk, plus-sign, question-mark, and left-brace are special except when -used in a bracket expression (see \fBRE Bracket Expression\fR, above). Any of -the following uses produce undefined results: -.RS +4 -.TP -.ie t \(bu -.el o -if these characters appear first in an ERE, or immediately following a -vertical-line, circumflex or left-parenthesis -.RE -.RS +4 -.TP -.ie t \(bu -.el o -if a left-brace is not part of a valid interval expression. -.RE -.RE - -.sp -.ne 2 -.na -\fB|\fR -.ad -.RS 14n -The vertical-line is special except when used in a bracket expression (see -\fBRE Bracket Expression\fR, above). A vertical-line appearing first or last in -an ERE, or immediately following a vertical-line or a left-parenthesis, or -immediately preceding a right-parenthesis, produces undefined results. -.RE - -.sp -.ne 2 -.na -\fB^\fR -.ad -.RS 14n -The circumflex is special when used: -.RS +4 -.TP -.ie t \(bu -.el o -as an anchor (see \fBERE Expression Anchoring\fR, below). -.RE -.RS +4 -.TP -.ie t \(bu -.el o -as the first character of a bracket expression (see \fBRE Bracket -Expression\fR, above). -.RE -.RE - -.sp -.ne 2 -.na -\fB$\fR -.ad -.RS 14n -The dollar sign is special when used as an anchor. -.RE - -.SS "Periods in EREs" -.LP -A period (\fB\&.\fR), when used outside a bracket expression, is an ERE that -matches any character in the supported character set except NUL. -.SS "ERE Bracket Expression" -.LP -The rules for ERE Bracket Expressions are the same as for Basic Regular -Expressions; see \fBRE Bracket Expression\fR, above). -.SS "EREs Matching Multiple Characters" -.LP -The following rules will be used to construct EREs matching multiple characters -from EREs matching a single character: -.RS +4 -.TP -1. -A \fIconcatenation\fR \fIof\fR \fIEREs\fR matches the concatenation of the -character sequences matched by each component of the ERE. A concatenation of -EREs enclosed in parentheses matches whatever the concatenation without the -parentheses matches. For example, both the ERE \fBcd\fR and the ERE \fB(cd)\fR -are matched by the third and fourth character of the string \fBabcdefabcdef\fR. -.RE -.RS +4 -.TP -2. -When an ERE matching a single character or an ERE enclosed in parentheses is -followed by the special character plus-sign (+), together with that plus-sign -it matches what one or more consecutive occurrences of the ERE would match. For -example, the ERE \fBb+(bc)\fR matches the fourth to seventh characters in the -string \fBacabbbcde;\fR \fB[ab]\fR + and \fB[ab][ab]*\fR are equivalent. -.RE -.RS +4 -.TP -3. -When an ERE matching a single character or an ERE enclosed in parentheses is -followed by the special character asterisk (\fB*\fR), together with that -asterisk it matches what zero or more consecutive occurrences of the ERE would -match. For example, the ERE \fBb*c\fR matches the first character in the string -\fBcabbbcde,\fR and the ERE \fBb*cd\fR matches the third to seventh characters -in the string \fBcabbbcdebbbbbbcdbc\fR. And, \fB[ab]*\fR and \fB[ab][ab]\fR are -equivalent when matching the string \fBab\fR. -.RE -.RS +4 -.TP -4. -When an ERE matching a single character or an ERE enclosed in parentheses is -followed by the special character question-mark (\fB?\fR), together with that -question-mark it matches what zero or one consecutive occurrences of the ERE -would match. For example, the ERE \fBb?c\fR matches the second character in the -string \fBacabbbcde\fR. -.RE -.RS +4 -.TP -5. -When an ERE matching a single character or an ERE enclosed in parentheses is -followed by an \fIinterval\fR \fIexpression\fR of the format {\fIm\fR}, -{\fIm\fR,} or {\fIm\fR,\fIn\fR}, together with that interval expression it -matches what repeated consecutive occurrences of the ERE would match. The -values of \fIm\fR and \fIn\fR will be decimal integers in the range 0 \(<= -\fIm\fR \(<= \fIn\fR \(<= \fB{RE_DUP_MAX}\fR, where \fIm\fR specifies the exact -or minimum number of occurrences and \fIn\fR specifies the maximum number of -occurrences. The expression {\fIm\fR} matches exactly \fIm\fR occurrences of -the preceding ERE, {\fIm\fR,} matches at least \fIm\fR occurrences and -{\fIm\fR,\fIn\fR} matches any number of occurrences between \fIm\fR and -\fIn\fR, inclusive. -.RE -.sp -.LP -For example, in the string \fBabababccccccd\fR the ERE c{3} is matched by -characters seven to nine and the ERE (ab){2,} is matched by characters one to -six. -.sp -.LP -The behavior of multiple adjacent duplication symbols \fB(\fR+\fB,\fR \fB*\fR, -\fB?\fR and intervals) produces undefined results. -.SS "ERE Alternation" -.LP -Two EREs separated by the special character vertical-line (|) match a string -that is matched by either. For example, the ERE a((bc)|d) matches the string -abc and the string ad. Single characters, or expressions matching single -characters, separated by the vertical bar and enclosed in parentheses, will be -treated as an ERE matching a single character. -.SS "ERE Precedence" -.LP -The order of precedence will be as shown in the following table: -.sp - -.sp -.TS -box; -l l -l l . -\fBERE Precedence (from high to low)\fR -collation-related bracket symbols [= =] [: :] [. .] -escaped characters \e<\fIspecial character\fR> -bracket expression [ ] -grouping ( ) -single-character-ERE duplication * + ? {\fIm\fR,\fIn\fR} -concatenation -anchoring ^ $ -alternation | -.TE - -.sp -.LP -For example, the ERE \fBabba\||\|cde\fR matches either the string \fBabba\fR or -the string \fBcde\fR (rather than the string \fBabbade\fR or \fBabbcde\fR, -because concatenation has a higher order of precedence than alternation). -.SS "ERE Expression Anchoring" -.LP -An ERE can be limited to matching strings that begin or end a line; this is -called \fIanchoring\fR. The circumflex and dollar sign special characters are -considered ERE anchors when used anywhere outside a bracket expression. This -has the following effects: -.RS +4 -.TP -1. -A circumflex (^) outside a bracket expression anchors the expression or -subexpression it begins to the beginning of a string; such an expression or -subexpression can match only a sequence starting at the first character of a -string. For example, the EREs ^ab and (^ab) match ab in the string abcdef, but -fail to match in the string cdefab, and the ERE a^b is valid, but can never -match because the \fBa\fR prevents the expression ^b from matching starting at -the first character. -.RE -.RS +4 -.TP -2. -A dollar sign \fB(\fR \fB$\fR \fB)\fR outside a bracket expression anchors -the expression or subexpression it ends to the end of a string; such an -expression or subexpression can match only a sequence ending at the last -character of a string. For example, the EREs ef$ and (ef$) match ef in the -string abcdef, but fail to match in the string cdefab, and the ERE e$f is -valid, but can never match because the \fBf\fR prevents the expression e$ from -matching ending at the last character. -.RE -.SH SEE ALSO -.LP -\fBlocaledef\fR(1), \fBregcomp\fR(3C), \fBattributes\fR(5), \fBenviron\fR(5), -\fBlocale\fR(5), \fBregexp\fR(5) +.\" +.\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for permission +.\" to reproduce portions of its copyrighted documentation. +.\" +.\" Original documentation from The Open Group can be obtained online at +.\" http://www.opengroup.org/bookstore/. +.\" +.\" The Institute of Electrical and Electronics Engineers and The Open Group, +.\" have given us permission to reprint portions of their documentation. In the +.\" following statement, the phrase "this text" refers to portions of the system +.\" documentation. +.\" +.\" Portions of this text are reprinted and reproduced in electronic form in the +.\" Sun OS Reference Manual, from IEEE Std 1003.1, 2004 Edition, Standard for +.\" Information Technology -- Portable Operating System Interface (POSIX), +.\" The Open Group Base Specifications Issue 6, Copyright (C) 2001-2004 by the +.\" Institute of Electrical and Electronics Engineers, Inc and The Open Group. +.\" +.\" In the event of any discrepancy between these versions and the original +.\" IEEE and The Open Group Standard, the original IEEE and The Open Group +.\" Standard is the referee document. +.\" +.\" The original Standard can be obtained online at +.\" http://www.opengroup.org/unix/online.html. +.\" +.\" This notice shall appear on any product containing this material. +.\" +.\" The contents of this file are subject to the terms of the +.\" Common Development and Distribution License (the "License"). +.\" You may not use this file except in compliance with the License. +.\" +.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE +.\" or http://www.opensolaris.org/os/licensing. +.\" See the License for the specific language governing permissions +.\" and limitations under the License. +.\" +.\" When distributing Covered Code, include this CDDL HEADER in each +.\" file and include the License file at usr/src/OPENSOLARIS.LICENSE. +.\" If applicable, add the following below this CDDL HEADER, with the +.\" fields enclosed by brackets "[]" replaced with your own identifying +.\" information: Portions Copyright [yyyy] [name of copyright owner] +.\" +.\" +.\" Copyright (c) 1992, X/Open Company Limited All Rights Reserved +.\" Portions Copyright (c) 1999, Sun Microsystems, Inc. All Rights Reserved +.\" Copyright 2017 Nexenta Systems, Inc. +.\" +.Dd June 2, 2017 +.Dt REGEX 5 +.Os +.Sh NAME +.Nm regex +.Nd internationalized basic and extended regular expression matching +.Sh DESCRIPTION +Regular Expressions +.Pq REs +provide a mechanism to select specific strings from a set of character strings. +The Internationalized Regular Expressions described below differ from the Simple +Regular Expressions described on the +.Xr regexp 5 +manual page in the following ways: +.Bl -bullet +.It +both Basic and Extended Regular Expressions are supported +.It +the Internationalization features -- character class, equivalence class, and +multi-character collation -- are supported. +.El +.Pp +The Basic Regular Expression +.Pq BRE +notation and construction rules described in the +.Sx BASIC REGULAR EXPRESSIONS +section apply to most utilities supporting regular expressions. +Some utilities, instead, support the Extended Regular Expressions +.Pq ERE +described in the +.Sx EXTENDED REGULAR EXPRESSIONS +section; any exceptions for both cases are noted in the descriptions of the +specific utilities using regular expressions. +Both BREs and EREs are supported by the Regular Expression Matching interfaces +.Xr regcomp 3C +and +.Xr regexec 3C . +.Sh BASIC REGULAR EXPRESSIONS +.Ss BREs Matching a Single Character +A BRE ordinary character, a special character preceded by a backslash, or a +period matches a single character. +A bracket expression matches a single character or a single collating element. +See +.Sx RE Bracket Expression , +below. +.Ss BRE Ordinary Characters +An ordinary character is a BRE that matches itself: any character in the +supported character set, except for the BRE special characters listed in +.Sx BRE Special Characters , +below. +.Pp +The interpretation of an ordinary character preceded by a backslash +.Pq Qq \e +is undefined, except for: +.Bl -enum +.It +the characters +.Qq \&) , +.Qq \&( , +.Qq { , +and +.Qq } +.It +the digits 1 to 9 inclusive +.Po see +.Sx BREs Matching Multiple Characters , +below +.Pc +.It +a character inside a bracket expression. +.El +.Ss BRE Special Characters +A BRE special character has special properties in certain contexts. +Outside those contexts, or when preceded by a backslash, such a character will +be a BRE that matches the special character itself. +The BRE special characters and the contexts in which they have their special +meaning are: +.Bl -tag -width Ds +.It Sy \&. \&[ \&\e +The period, left-bracket, and backslash are special except when used in a +bracket expression +.Po see +.Sx RE Bracket Expression , +below +.Pc . +An expression containing a +.Qq \&[ +that is not preceded by a backslash and is not part of a bracket expression +produces undefined results. +.It Sy * +The asterisk is special except when used: +.Bl -bullet +.It +in a bracket expression +.It +as the first character of an entire BRE +.Po after an initial +.Qq ^ , +if any +.Pc +.It +as the first character of a subexpression +.Po after an initial +.Qq ^ , +if any; see +.Sx BREs Matching Multiple Characters , +below +.Pc . +.El +.It Sy ^ +The circumflex is special when used: +.Bl -bullet +.It +as an anchor +.Po see +.Sx BRE Expression Anchoring , +below +.Pc . +.It +as the first character of a bracket expression +.Po see +.Sx RE Bracket Expression , +below +.Pc . +.El +.It Sy $ +The dollar sign is special when used as an anchor. +.El +.Ss Periods in BREs +A period +.Pq Qq \&. , +when used outside a bracket expression, is a BRE that matches any character in +the supported character set except NUL. +.Ss RE Bracket Expression +A bracket expression +.Po an expression enclosed in square brackets, +.Qq [] +.Pc +is an RE that matches a single collating element contained in the non-empty set +of collating elements represented by the bracket expression. +.Pp +The following rules and definitions apply to bracket expressions: +.Bl -enum +.It +A +.Em bracket expression +is either a matching list expression or a non-matching list expression. +It consists of one or more expressions: collating elements, collating symbols, +equivalence classes, character classes, or range expressions +.Pq see rule 7 below . +Portable applications must not use range expressions, even though all +implementations support them. +The right-bracket +.Pq Qq \&] +loses its special meaning and represents itself in a bracket expression if it +occurs first in the list +.Po after an initial circumflex +.Pq Qq ^ , +if any +.Pc . +Otherwise, it terminates the bracket expression, unless it appears in a +collating symbol +.Po such as +.Qq [.].] +.Pc +or is the ending right-bracket for a collating symbol, equivalence class, or +character class. +.Pp +The special characters +.Qq \&. , +.Qq * , +.Qq \&[ , +.Qq \&\e +.Pq period, asterisk, left-bracket and backslash, respectively +lose their special meaning within a bracket expression. +.Pp +The character sequences +.Qq [. , +.Qq [= , +.Qq [: +.Pq left-bracket followed by a period, equals-sign, or colon +are special inside a bracket expression and are used to delimit collating +symbols, equivalence class expressions, and character class expressions. +These symbols must be followed by a valid expression and the matching +terminating sequence +.Qq .] , +.Qq =] +or +.Qq :] , +as described in the following items. +.It +A +.Em matching list expression +specifies a list that matches any one of the expressions represented in the +list. +The first character in the list must not be the circumflex. +For example, +.Qq [abc] +is an RE that matches any of the characters +.Qq a , +.Qq b +or +.Qq c . +.It +A +.Em non-matching list expression +begins with a circumflex +.Pq Qq ^ , +and specifies a list that matches any character or collating element except for +the expressions represented in the list after the leading circumflex. +For example, +.Qq [^abc] +is an RE that matches any character or collating element except the characters +.Qq a , +.Qq b , +or +.Qq c . +The circumflex will have this special meaning only when it occurs first in the +list, immediately following the left-bracket. +.It +A +.Em collating symbol +is a collating element enclosed within bracket-period +.Pq Qq [..] +delimiters. +Multi-character collating elements must be represented as collating symbols when +it is necessary to distinguish them from a list of the individual characters +that make up the multi-character collating element. +For example, if the string +.Qq ch +is a collating element in the current collation sequence with the associated +collating symbol +.Qq Aq ch , +the expression +.Qq [[.ch.]] +will be treated as an RE matching the character sequence +.Qq ch , +while +.Qq [ch] +will be treated as an RE matching +.Qq c +or +.Qq h . +Collating symbols will be recognized only inside bracket expressions. +This implies that the RE +.Qq [[.ch.]]*c +matches the first to fifth character in the string +.Qq chchch. +If the string is not a collating element in the current collating sequence +definition, or if the collating element has no characters associated with it, +the symbol will be treated as an invalid expression. +.It +An +.Em equivalence class expression +represents the set of collating elements belonging to an equivalence class. +Only primary equivalence classes will be recognised. +The class is expressed by enclosing any one of the collating elements in the +equivalence class within bracket-equal +.Pq Qq [==] +delimiters. +For example, if +.Qq a +and +.Qq b +belong to the same equivalence class, then +.Qq [[=a=]b] , +.Qq [[==]a] +and +.Qq [[==]b] +will each be equivalent to +.Qq [ab] . +If the collating element does not belong to an equivalence class, the +equivalence class expression will be treated as a +.Em collating symbol . +.It +A +.Em character class expression +represents the set of characters belonging to a character class, as defined in +the +.Ev LC_CTYPE +category in the current locale. +All character classes specified in the current locale will be recognized. +A character class expression is expressed as a character class name enclosed +within bracket-colon +.Pq Qq [::] +delimiters. +.Pp +The following character class expressions are supported in all locales: +.Bl -column "[:alnum:]" "[:cntrl:]" "[:lower:]" "[:xdigit:]" +.It [:alnum:] Ta [:cntrl:] Ta [:lower:] Ta [:space:] +.It [:alpha:] Ta [:digit:] Ta [:print:] Ta [:upper:] +.It [:blank:] Ta [:graph:] Ta [:punct:] Ta [:xdigit:] +.El +.Pp +In addition, character class expressions of the form +.Qq [:name:] +are recognized in those locales where the +.Em name +keyword has been given a +.Em charclass +definition in the +.Ev LC_CTYPE +category. +.It +A +.Em range expression +represents the set of collating elements that fall between two elements in the +current collation sequence, inclusively. +It is expressed as the starting point and the ending point separated by a hyphen +.Pq Qq - . +.Pp +Range expressions must not be used in portable applications because their +behavior is dependent on the collating sequence. +Ranges will be treated according to the current collating sequence, and include +such characters that fall within the range based on that collating sequence, +regardless of character values. +This, however, means that the interpretation will differ depending on collating +sequence. +If, for instance, one collating sequence defines as a variant of +.Qq a , +while another defines it as a letter following +.Qq z , +then the expression +.Qq [-z] +is valid in the first language and invalid in the second. +.sp +In the following, all examples assume the collation sequence specified for the +POSIX locale, unless another collation sequence is specifically defined. +.Pp +The starting range point and the ending range point must be a collating element +or collating symbol. +An equivalence class expression used as a starting or ending point of a range +expression produces unspecified results. +An equivalence class can be used portably within a bracket expression, but only +outside the range. +For example, the unspecified expression +.Qq [[=e=]-f] +should be given as +.Qq [[=e=]e-f] . +The ending range point must collate equal to or higher than the starting range +point; otherwise, the expression will be treated as invalid. +The order used is the order in which the collating elements are specified in the +current collation definition. +One-to-many mappings +.Po see +.Xr locale 5 +.Pc +will not be performed. +For example, assuming that the character +.Qq eszet +is placed in the collation sequence after +.Qq r +and +.Qq s , +but before +.Qq t , +and that it maps to the sequence +.Qq ss +for collation purposes, then the expression +.Qq [r-s] +matches only +.Qq r +and +.Qq s , +but the expression +.Qq [s-t] +matches +.Qq s , +.Qq beta , +or +.Qq t . +.Pp +The interpretation of range expressions where the ending range point is also +the starting range point of a subsequent range expression +.Po for instance +.Qq [a-m-o] +.Pc +is undefined. +.Pp +The hyphen character will be treated as itself if it occurs first +.Po after an initial +.Qq ^ , +if any +.Pc +or last in the list, or as an ending range point in a range expression. +As examples, the expressions +.Qq [-ac] +and +.Qq [ac-] +are equivalent and match any of the characters +.Qq a , +.Qq c , +or +.Qq -; +.Qq [^-ac] +and +.Qq [^ac-] +are equivalent and match any characters except +.Qq a , +.Qq c , +or +.Qq -; +the expression +.Qq [%--] +matches any of the characters between +.Qq % +and +.Qq - +inclusive; the expression +.Qq [--@] +matches any of the characters between +.Qq - +and +.Qq @ +inclusive; and the expression +.Qq [a--@] +is invalid, because the letter +.Qq a +follows the symbol +.Qq - +in the POSIX locale. +To use a hyphen as the starting range point, it must either come first in the +bracket expression or be specified as a collating symbol, for example: +.Qq [][.-.]-0] , +which matches either a right bracket or any character or collating element that +collates between hyphen and 0, inclusive. +.Pp +If a bracket expression must specify both +.Qq - +and +.Qq \&] , +the +.Qq \&] +must be placed first +.Po after the +.Qq ^ , +if any +.Pc +and the +.Qq - +last within the bracket expression. +.El +.Pp +Note: Latin-1 characters such as +.Qq \(ga +or +.Qq ^ +are not printable in some locales, for example, the +.Em ja +locale. +.Ss BREs Matching Multiple Characters +The following rules can be used to construct BREs matching multiple characters +from BREs matching a single character: +.Bl -enum +.It +The concatenation of BREs matches the concatenation of the strings matched +by each component of the BRE. +.It +A +.Em subexpression +can be defined within a BRE by enclosing it between the character pairs +.Qq \e( +and +.Qq \e) . +Such a subexpression matches whatever it would have matched without the +.Qq \e( +and +Qq \e) , +except that anchoring within subexpressions is optional behavior; see +.Sx BRE Expression Anchoring , +below. +Subexpressions can be arbitrarily nested. +.It +The +.Em back-reference +expression +.Qq \e Ns Em n +matches the same +.Pq possibly empty +string of characters as was matched by a subexpression enclosed between +.Qq \e( +and +.Qq \e) +preceding the +.Qq \e Ns Em n . +The character +.Qq Em n +must be a digit from 1 to 9 inclusive, +.Em n Ns th +subexpression +.Po the one that begins with the +.Em n Ns th +.Qq \e( +and ends with the corresponding paired +.Qq \e) +.Pc . +The expression is invalid if less than +.Em n +subexpressions precede the +.Qq \e Ns Em n . +For example, the expression +.Qq ^\e(.*\e)\e1$ +matches a line consisting of two adjacent appearances of the same string, and +the expression +.Qq \e(a\e)*\e1 +fails to match +.Qq a . +The limit of nine back-references to subexpressions in the RE is based on the +use of a single digit identifier. +This does not imply that only nine subexpressions are allowed in REs. +.It +When a BRE matching a single character, a subexpression or a back-reference is +followed by the special character asterisk +.Pq Qq * , +together with that asterisk it matches what zero or more consecutive occurrences +of the BRE would match. +For example, +.Qq [ab]* +and +.Qq [ab][ab] +are equivalent when matching the string +.Qq ab . +.It +When a BRE matching a single character, a subexpression, or a back-reference +is followed by an +.Em interval expression +of the format +.Qq \e{ Ns Em m Ns \e} , +.Qq \e{ Ns Em m Ns ,\e} +or +.Qq \e{ Ns Em m Ns , Ns Em n Ns \e} , +together with that interval expression it matches what repeated consecutive +occurrences of the BRE would match. +The values of +.Em m +and +.Em n +will be decimal integers in the range 0 <= +.Em m +<= +.Em n +<= +.Dv BRE_DUP_MAX , +where +.Em m +specifies the exact or minimum number of occurrences and +.Em n +specifies the maximum number of occurrences. +The expression +.Qq \e{ Ns Em m Ns \e} +matches exactly +.Em m +occurrences of the preceding BRE, +.Qq \e{ Ns Em m Ns ,\e} +matches at least +.Em m +occurrences and +.Qq \e{ Ns Em m Ns , Ns Em n Ns \e} +matches any number of occurrences between +.Em m +and +.Em n , +inclusive. +.Pp +For example, in the string +.Qq abababccccccd , +the BRE +.Qq c\e{3\e} +is matched by characters seven to nine, the BRE +.Qq \e(ab\e)\e{4,\e} +is not matched at all and the BRE +.Qq c\e{1,3\e}d +is matched by characters ten to thirteen. +.El +.Pp +The behavior of multiple adjacent duplication symbols +.Po Qq * +and intervals +.Pc +produces undefined results. +.Ss BRE Precedence +The order of precedence is as shown in the following table: +.Bl -column "BRE Precedence (from high to low)" "" +.It Sy BRE Precedence (from high to low) Ta +.It collation-related bracket symbols Ta [= =] [: :] [. .] +.It escaped characters Ta \e< Ns Em special character Ns > +.It bracket expression Ta [ ] +.It subexpressions/back-references Ta \e( \e) \e Ns Em n +.It single-character-BRE duplication Ta * \e{ Ns Em m Ns , Ns Em n Ns \e} +.It concatenation Ta +.It anchoring Ta ^ $ +.El +.Ss BRE Expression Anchoring +A BRE can be limited to matching strings that begin or end a line; this is +called +.Em anchoring . +The circumflex and dollar sign special characters will be considered BRE anchors +in the following contexts: +.Bl -enum +.It +A circumflex +.Pq Qq ^ +is an anchor when used as the first character of an entire BRE. +The implementation may treat circumflex as an anchor when used as the first +character of a subexpression. +The circumflex will anchor the expression to the beginning of a string; +only sequences starting at the first character of a string will be matched by +the BRE. +For example, the BRE +.Qq ^ab +matches +.Qq ab +in the string +.Qq abcdef , +but fails to match in the string +.Qq cdefab . +A portable BRE must escape a leading circumflex in a subexpression to match a +literal circumflex. +.It +A dollar sign +.Pq Qq $ +is an anchor when used as the last character of an entire BRE. +The implementation may treat a dollar sign as an anchor when used as the last +character of a subexpression. +The dollar sign will anchor the expression to the end of the string being +matched; the dollar sign can be said to match the end-of-string following the +last character. +.It +A BRE anchored by both +.Qq ^ +and +.Qq $ +matches only an entire string. +For example, the BRE +^abcdef$ +matches strings consisting only of +.Qq abcdef . +.It +.Qq ^ +and +.Qq $ +are not special in subexpressions. +.El +.Pp +Note: The Solaris implementation does not support anchoring in BRE +subexpressions. +.Sh EXTENDED REGULAR EXPRESSIONS +The rules specified for BREs apply to Extended Regular Expressions +.Pq EREs +with the following exceptions: +.Bl -bullet +.It +The characters +.Qq | , +.Qq + , +and +.Qq \&? +have special meaning, as defined below. +.It +The +.Qq { +and +.Qq } +characters, when used as the duplication operator, are not preceded by +backslashes. +The constructs +.Qq \e{ +and +.Qq \e} +simply match the characters +.Qq { +and +.Qq }, respectively. +.It +The back reference operator is not supported. +.It +Anchoring +.Pq Qq ^$ +is supported in subexpressions. +.El +.Ss EREs Matching a Single Character +An ERE ordinary character, a special character preceded by a backslash, or a +period matches a single character. +A bracket expression matches a single character or a single collating element. +An +.Em ERE matching a single character +enclosed in parentheses matches the same as the ERE without parentheses would +have matched. +.Ss ERE Ordinary Characters +An +.Em ordinary character +is an ERE that matches itself. +An ordinary character is any character in the supported character set, except +for the ERE special characters listed in +.Sx ERE Special Characters +below. +The interpretation of an ordinary character preceded by a backslash +.Pq Qq \&\e +is undefined. +.Ss ERE Special Characters +An +.Em ERE special character +has special properties in certain contexts. +Outside those contexts, or when preceded by a backslash, such a character is an +ERE that matches the special character itself. +The extended regular expression special characters and the contexts in which +they have their special meaning are: +.Bl -tag -width Ds +.It Sy \&. \&[ \&\e \&( +The period, left-bracket, backslash, and left-parenthesis are special except +when used in a bracket expression +.Po see +.Sx RE Bracket Expression , +above +.Pc . +Outside a bracket expression, a left-parenthesis immediately followed by a +right-parenthesis produces undefined results. +.It Sy \&) +The right-parenthesis is special when matched with a preceding +left-parenthesis, both outside a bracket expression. +.It Sy * + \&? { +The asterisk, plus-sign, question-mark, and left-brace are special except when +used in a bracket expression +.Po see +.Sx RE Bracket Expression , +above +.Pc . +Any of the following uses produce undefined results: +.Bl -bullet +.It +if these characters appear first in an ERE, or immediately following a +vertical-line, circumflex or left-parenthesis +.It +if a left-brace is not part of a valid interval expression. +.El +.It Sy \&| +The vertical-line is special except when used in a bracket expression +.Po see +.Sx RE Bracket Expression , +above +.Pc . +A vertical-line appearing first or last in an ERE, or immediately following a +vertical-line or a left-parenthesis, or immediately preceding a +right-parenthesis, produces undefined results. +.It Sy ^ +The circumflex is special when used: +.Bl -bullet +.It +as an anchor +.Po see +.Sx ERE Expression Anchoring , +below +.Pc . +.It +as the first character of a bracket expression +.Po see +.Sx RE Bracket Expression , +above +.Pc . +.El +.It Sy $ +The dollar sign is special when used as an anchor. +.El +.Ss Periods in EREs +A period +.Pq Qq \&. , +when used outside a bracket expression, is an ERE that matches any character in +the supported character set except NUL. +.Ss ERE Bracket Expression +The rules for ERE Bracket Expressions are the same as for Basic Regular +Expressions; see +.Sx RE Bracket Expression , +above. +.Ss EREs Matching Multiple Characters +The following rules will be used to construct EREs matching multiple characters +from EREs matching a single character: +.Bl -enum +.It +A +.Em concatenation of EREs +matches the concatenation of the character sequences matched by each component +of the ERE. +A concatenation of EREs enclosed in parentheses matches whatever the +concatenation without the parentheses matches. +For example, both the ERE +.Qq cd +and the ERE +.Qq (cd) +are matched by the third and fourth character of the string +.Qq abcdefabcdef . +.It +When an ERE matching a single character or an ERE enclosed in parentheses is +followed by the special character plus-sign +.Pq Qq + , +together with that plus-sign it matches what one or more consecutive occurrences +of the ERE would match. +For example, the ERE +.Qq b+(bc) +matches the fourth to seventh characters in the string +.Qq acabbbcde ; +.Qq [ab]+ +and +.Qq [ab][ab]* +are equivalent. +.It +When an ERE matching a single character or an ERE enclosed in parentheses is +followed by the special character asterisk +.Pq Qq * , +together with that asterisk it matches what zero or more consecutive occurrences +of the ERE would match. +For example, the ERE +.Qq b*c +matches the first character in the string +.Qq cabbbcde , +and the ERE +.Qq b*cd +matches the third to seventh characters in the string +.Qq cabbbcdebbbbbbcdbc . +And, +.Qq [ab]* +and +.Qq [ab][ab] +are equivalent when matching the string +.Qq ab . +.It +When an ERE matching a single character or an ERE enclosed in parentheses is +followed by the special character question-mark +.Pq Qq \&? , +together with that question-mark it matches what zero or one consecutive +occurrences of the ERE would match. +For example, the ERE +.Qq b?c +matches the second character in the string +.Qq acabbbcde . +.It +When an ERE matching a single character or an ERE enclosed in parentheses is +followed by an +.Em interval expression +of the format +.Qq { Ns Em m Ns } , +.Qq { Ns Em m Ns ,} +or +.Qq { Ns Em m Ns , Ns Em n Ns } , +together with that interval expression it matches what repeated consecutive +occurrences of the ERE would match. +The values of +.Em m +and +.Em n +will be decimal integers in the range 0 <= +.Em m +<= +.Em n +<= +.Dv RE_DUP_MAX , +where +.Em m +specifies the exact or minimum number of occurrences and +.Em n +specifies the maximum number of occurrences. +The expression +.Qq { Ns Em m Ns } +matches exactly +.Em m +occurrences of the preceding ERE, +.Qq { Ns Em m Ns ,} +matches at least +.Em m +occurrences and +.Qq { Ns m Ns , Ns Em n Ns } +matches any number of occurrences between +.Em m +and +.Em n , +inclusive. +.El +.Pp +For example, in the string +.Qq abababccccccd +the ERE +.Qq c{3} +is matched by characters seven to nine and the ERE +.Qq (ab){2,} +is matched by characters one to six. +.Pp +The behavior of multiple adjacent duplication symbols +.Po +.Qq + , +.Qq * , +.Qq \&? +and intervals +.Pc +produces undefined results. +.Ss ERE Alternation +Two EREs separated by the special character vertical-line +.Pq Qq | +match a string that is matched by either. +For example, the ERE +.Qq a((bc)|d) +matches the string +.Qq abc +and the string +.Qq ad . +Single characters, or expressions matching single characters, separated by the +vertical bar and enclosed in parentheses, will be treated as an ERE matching a +single character. +.Ss ERE Precedence +The order of precedence will be as shown in the following table: +.Bl -column "ERE Precedence (from high to low)" "" +.It Sy ERE Precedence (from high to low) Ta +.It collation-related bracket symbols Ta [= =] [: :] [. .] +.It escaped characters Ta \e< Ns Em special character Ns > +.It bracket expression Ta \&[ \&] +.It grouping Ta \&( \&) +.It single-character-ERE duplication Ta * + \&? { Ns Em m Ns , Ns Em n Ns} +.It concatenation Ta +.It anchoring Ta ^ $ +.It alternation Ta | +.El +.Pp +For example, the ERE +.Qq abba|cde +matches either the string +.Qq abba +or the string +.Qq cde +.Po rather than the string +.Qq abbade +or +.Qq abbcde , +because concatenation has a higher order of precedence than alternation +.Pc . +.Ss ERE Expression Anchoring +An ERE can be limited to matching strings that begin or end a line; this is +called +.Em anchoring . +The circumflex and dollar sign special characters are considered ERE anchors +when used anywhere outside a bracket expression. +This has the following effects: +.Bl -enum +.It +A circumflex +.Pq Qq ^ +outside a bracket expression anchors the expression or subexpression it begins +to the beginning of a string; such an expression or subexpression can match only +a sequence starting at the first character of a string. +For example, the EREs +.Qq ^ab +and +.Qq (^ab) +match +.Qq ab +in the string +.Qq abcdef , +but fail to match in the string +.Qq cdefab , +and the ERE +.Qq a^b +is valid, but can never match because the +.Qq a +prevents the expression +.Qq ^b +from matching starting at the first character. +.It +A dollar sign +.Pq Qq $ +outside a bracket expression anchors the expression or subexpression it ends to +the end of a string; such an expression or subexpression can match only a +sequence ending at the last character of a string. +For example, the EREs +.Qq ef$ +and +.Qq (ef$) +match +.Qq ef +in the string +.Qq abcdef , +but fail to match in the string +.Qq cdefab , +and the ERE +.Qq e$f +is valid, but can never match because the +.Qq f +prevents the expression +.Qq e$ +from matching ending at the last character. +.El +.Sh SEE ALSO +.Xr localedef 1 , +.Xr regcomp 3C , +.Xr attributes 5 , +.Xr environ 5 , +.Xr locale 5 , +.Xr regexp 5 -- 2.11.4.GIT