1.\" $OpenBSD: re_format.7,v 1.10 2003/06/02 20:18:36 millert Exp $ 2.\" 3.\" Copyright (c) 1997, Phillip F Knaack. All rights reserved. 4.\" 5.\" Copyright (c) 1992, 1993, 1994 Henry Spencer. 6.\" Copyright (c) 1992, 1993, 1994 7.\" The Regents of the University of California. All rights reserved. 8.\" 9.\" This code is derived from software contributed to Berkeley by 10.\" Henry Spencer. 11.\" 12.\" Redistribution and use in source and binary forms, with or without 13.\" modification, are permitted provided that the following conditions 14.\" are met: 15.\" 1. Redistributions of source code must retain the above copyright 16.\" notice, this list of conditions and the following disclaimer. 17.\" 2. Redistributions in binary form must reproduce the above copyright 18.\" notice, this list of conditions and the following disclaimer in the 19.\" documentation and/or other materials provided with the distribution. 20.\" 3. Neither the name of the University nor the names of its contributors 21.\" may be used to endorse or promote products derived from this software 22.\" without specific prior written permission. 23.\" 24.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34.\" SUCH DAMAGE. 35.\" 36.\" @(#)re_format.7 8.3 (Berkeley) 3/20/94 37.\" 38.Dd March 20, 1994 39.Dt RE_FORMAT 7 40.Os 41.Sh NAME 42.Nm re_format 43.Nd POSIX 1003.2 regular expressions 44.Sh DESCRIPTION 45Regular expressions (``RE''s), 46as defined in POSIX 1003.2, come in two forms: 47modern REs (roughly those of 48.Xr egrep 1 ; 491003.2 calls these ``extended'' REs) 50and obsolete REs (roughly those of 51.Xr ed 1 ; 521003.2 ``basic'' REs). 53Obsolete REs mostly exist for backward compatibility in some old programs; 54they will be discussed at the end. 551003.2 leaves some aspects of RE syntax and semantics open; 56`\(dg' marks decisions on these aspects that 57may not be fully portable to other 1003.2 implementations. 58.Pp 59A (modern) RE is one\(dg or more non-empty\(dg 60.Em branches , 61separated by `|'. 62It matches anything that matches one of the branches. 63.Pp 64A branch is one\(dg or more 65.Em pieces , 66concatenated. 67It matches a match for the first, followed by a match for the second, etc. 68.Pp 69A piece is an 70.Em atom 71possibly followed by a single\(dg `*', `+', `?', or 72.Em bound . 73An atom followed by `*' matches a sequence of 0 or more matches of the atom. 74An atom followed by `+' matches a sequence of 1 or more matches of the atom. 75An atom followed by `?' matches a sequence of 0 or 1 matches of the atom. 76.Pp 77A 78.Em bound 79is `{' followed by an unsigned decimal integer, 80possibly followed by `,' 81possibly followed by another unsigned decimal integer, 82always followed by `}'. 83The integers must lie between 0 and RE_DUP_MAX (255\(dg) inclusive, 84and if there are two of them, the first may not exceed the second. 85An atom followed by a bound containing one integer \fIi\fR 86and no comma matches 87a sequence of exactly \fIi\fR matches of the atom. 88An atom followed by a bound 89containing one integer \fIi\fR and a comma matches 90a sequence of \fIi\fR or more matches of the atom. 91An atom followed by a bound 92containing two integers \fIi\fR and \fIj\fR matches 93a sequence of \fIi\fR through \fIj\fR (inclusive) matches of the atom. 94.Pp 95An 96.Em atom 97is a regular expression enclosed in `()' 98(matching a match for the regular expression), 99an empty set of `()' (matching the null string)\(dg, 100a 101.Em "bracket expression" 102(see below), `.' 103(matching any single character), `^' (matching the null string at the 104beginning of a line), `$' (matching the null string at the 105end of a line), a `\e' followed by one of the characters 106`^.[$()|*+?{\e' 107(matching that character taken as an ordinary character), 108a `\e' followed by any other character\(dg 109(matching that character taken as an ordinary character, 110as if the `\e' had not been present\(dg), 111or a single character with no other significance (matching that character). 112A `{' followed by a character other than a digit is an ordinary 113character, not the beginning of a bound\(dg. 114It is illegal to end an RE with `\e'. 115.Pp 116A 117.Em "bracket expression" 118is a list of characters enclosed in `[]'. 119It normally matches any single character from the list (but see below). 120If the list begins with `^', 121it matches any single character 122(but see below) 123.Em not 124from the rest of the list. 125If two characters in the list are separated by `\-', this is shorthand 126for the full 127.Em range 128of characters between those two (inclusive) in the 129collating sequence, 130e.g., `[0-9]' in ASCII matches any decimal digit. 131It is illegal\(dg for two ranges to share an 132endpoint, e.g., `a-c-e'. 133Ranges are very collating-sequence-dependent, 134and portable programs should avoid relying on them. 135.Pp 136To include a literal `]' in the list, make it the first character 137(following a possible `^'). 138To include a literal `\-', make it the first or last character, 139or the second endpoint of a range. 140To use a literal `\-' as the first endpoint of a range, 141enclose it in `[.' and `.]' to make it a collating element (see below). 142With the exception of these and some combinations using `[' (see next 143paragraphs), all other special characters, including `\e', lose their 144special significance within a bracket expression. 145.Pp 146Within a bracket expression, a collating element (a character, 147a multi-character sequence that collates as if it were a single character, 148or a collating-sequence name for either) 149enclosed in `[.' and `.]' stands for the 150sequence of characters of that collating element. 151The sequence is a single element of the bracket expression's list. 152A bracket expression containing a multi-character collating element 153can thus match more than one character, 154e.g., if the collating sequence includes a `ch' collating element, 155then the RE `[[.ch.]]*c' matches the first five characters 156of `chchcc'. 157.Pp 158Within a bracket expression, a collating element enclosed in `[=' and 159`=]' is an equivalence class, standing for the sequences of characters 160of all collating elements equivalent to that one, including itself. 161(If there are no other equivalent collating elements, 162the treatment is as if the enclosing delimiters were `[.' and `.]'.) 163For example, if o and \o'o^' are the members of an equivalence class, 164then `[[=o=]]', `[[=\o'o^'=]]', and `[o\o'o^']' are all synonymous. 165An equivalence class may not\(dg be an endpoint 166of a range. 167.Pp 168Within a bracket expression, the name of a 169.Em "character class" 170enclosed 171in `[:' and `:]' stands for the list of all characters belonging to that 172class. 173Standard character class names are: 174.Pp 175.Bl -item -compact -offset indent 176.It 177alnum digit punct 178.It 179alpha graph space 180.It 181blank lower upper 182.It 183cntrl print xdigit 184.El 185.Pp 186These stand for the character classes defined in 187.Xr ctype 3 . 188A locale may provide others. 189A character class may not be used as an endpoint of a range. 190.Pp 191There are two special cases\(dg of bracket expressions: 192the bracket expressions `[[:<:]]' and `[[:>:]]' match the null string at 193the beginning and end of a word respectively. 194A word is defined as a sequence of 195word characters 196which is neither preceded nor followed by 197word characters. 198A word character is an 199.Em alnum 200character (as defined by 201.Xr ctype 3 ) 202or an underscore. 203This is an extension, 204compatible with but not specified by POSIX 1003.2, 205and should be used with 206caution in software intended to be portable to other systems. 207.Pp 208In the event that an RE could match more than one substring of a given 209string, 210the RE matches the one starting earliest in the string. 211If the RE could match more than one substring starting at that point, 212it matches the longest. 213Subexpressions also match the longest possible substrings, subject to 214the constraint that the whole match be as long as possible, 215with subexpressions starting earlier in the RE taking priority over 216ones starting later. 217Note that higher-level subexpressions thus take priority over 218their lower-level component subexpressions. 219.Pp 220Match lengths are measured in characters, not collating elements. 221A null string is considered longer than no match at all. 222For example, 223`bb*' matches the three middle characters of `abbbc', 224`(wee|week)(knights|nights)' matches all ten characters of `weeknights', 225when `(.*).*' is matched against `abc' the parenthesized subexpression 226matches all three characters, and 227when `(a*)*' is matched against `bc' both the whole RE and the parenthesized 228subexpression match the null string. 229.Pp 230If case-independent matching is specified, 231the effect is much as if all case distinctions had vanished from the 232alphabet. 233When an alphabetic that exists in multiple cases appears as an 234ordinary character outside a bracket expression, it is effectively 235transformed into a bracket expression containing both cases, 236e.g., `x' becomes `[xX]'. 237When it appears inside a bracket expression, all case counterparts 238of it are added to the bracket expression, so that (e.g.) `[x]' 239becomes `[xX]' and `[^x]' becomes `[^xX]'. 240.Pp 241No particular limit is imposed on the length of REs\(dg. 242Programs intended to be portable should not employ REs longer 243than 256 bytes, 244as an implementation can refuse to accept such REs and remain 245POSIX-compliant. 246.Pp 247Obsolete (``basic'') regular expressions differ in several respects. 248`|', `+', and `?' are ordinary characters and there is no equivalent 249for their functionality. 250The delimiters for bounds are `\e{' and `\e}', 251with `{' and `}' by themselves ordinary characters. 252The parentheses for nested subexpressions are `\e(' and `\e)', 253with `(' and `)' by themselves ordinary characters. 254`^' is an ordinary character except at the beginning of the 255RE or\(dg the beginning of a parenthesized subexpression, 256`$' is an ordinary character except at the end of the 257RE or\(dg the end of a parenthesized subexpression, 258and `*' is an ordinary character if it appears at the beginning of the 259RE or the beginning of a parenthesized subexpression 260(after a possible leading `^'). 261Finally, there is one new type of atom, a 262.Em "back reference" : 263`\e' followed by a non-zero decimal digit 264.Em d 265matches the same sequence of characters 266matched by the 267.Em d Ns th 268parenthesized subexpression 269(numbering subexpressions by the positions of their opening parentheses, 270left to right), 271so that (e.g.) `\e([bc]\e)\e1' matches `bb' or `cc' but not `bc'. 272.Sh SEE ALSO 273.Xr regex 3 274.Pp 275POSIX 1003.2, section 2.8 (Regular Expression Notation). 276.Sh BUGS 277Having two kinds of REs is a botch. 278.Pp 279The current 1003.2 spec says that `)' is an ordinary character in 280the absence of an unmatched `('; 281this was an unintentional result of a wording error, 282and change is likely. 283Avoid relying on it. 284.Pp 285Back references are a dreadful botch, 286posing major problems for efficient implementations. 287They are also somewhat vaguely defined 288(does 289`a\e(\e(b\e)*\e2\e)*d' match `abbbd'?). 290Avoid using them. 291.Pp 2921003.2's specification of case-independent matching is vague. 293The ``one case implies all cases'' definition given above 294is current consensus among implementors as to the right interpretation. 295.Pp 296The syntax for word boundaries is incredibly ugly. 297