1 .\" From Henry Spencer's regex package (as found in the apache
2 .\" distribution). The package carries the following copyright:
4 .\" Copyright 1992, 1993, 1994 Henry Spencer. All rights reserved.
5 .\" %%%LICENSE_START(MISC)
6 .\" This software is not subject to any license of the American Telephone
7 .\" and Telegraph Company or of the Regents of the University of California.
9 .\" Permission is granted to anyone to use this software for any purpose
10 .\" on any computer system, and to alter it and redistribute it, subject
11 .\" to the following restrictions:
13 .\" 1. The author is not responsible for the consequences of use of this
14 .\" software, no matter how awful, even if they arise from flaws in it.
16 .\" 2. The origin of this software must not be misrepresented, either by
17 .\" explicit claim or by omission. Since few users ever read sources,
18 .\" credits must appear in the documentation.
20 .\" 3. Altered versions must be plainly marked as such, and must not be
21 .\" misrepresented as being the original software. Since few users
22 .\" ever read sources, credits must appear in the documentation.
24 .\" 4. This notice may not be removed or altered.
27 .\" In order to comply with `credits must appear in the documentation'
28 .\" I added an AUTHOR paragraph below - aeb.
30 .\" In the default nroff environment there is no dagger \(dg.
32 .\" 2005-05-11 Removed discussion of `[[:<:]]' and `[[:>:]]', which
33 .\" appear not to be in the glibc implementation of regcomp
37 .TH REGEX 7 2020-08-13 "" "Linux Programmer's Manual"
39 regex \- POSIX.2 regular expressions
41 Regular expressions ("RE"s),
42 as defined in POSIX.2, come in two forms:
43 modern REs (roughly those of
45 POSIX.2 calls these "extended" REs)
46 and obsolete REs (roughly those of
49 Obsolete REs mostly exist for backward compatibility in some old programs;
50 they will be discussed at the end.
51 POSIX.2 leaves some aspects of RE syntax and semantics open;
52 "\*(dg" marks decisions on these aspects that
53 may not be fully portable to other POSIX.2 implementations.
55 A (modern) RE is one\*(dg or more nonempty\*(dg \fIbranches\fR,
56 separated by \(aq|\(aq.
57 It matches anything that matches one of the branches.
59 A branch is one\*(dg or more \fIpieces\fR, concatenated.
60 It matches a match for the first, followed by a match for the second,
63 A piece is an \fIatom\fR possibly followed
64 by a single\*(dg \(aq*\(aq, \(aq+\(aq, \(aq?\(aq, or \fIbound\fR.
65 An atom followed by \(aq*\(aq
66 matches a sequence of 0 or more matches of the atom.
67 An atom followed by \(aq+\(aq
68 matches a sequence of 1 or more matches of the atom.
69 An atom followed by \(aq?\(aq
70 matches a sequence of 0 or 1 matches of the atom.
72 A \fIbound\fR is \(aq{\(aq followed by an unsigned decimal integer,
73 possibly followed by \(aq,\(aq
74 possibly followed by another unsigned decimal integer,
75 always followed by \(aq}\(aq.
76 The integers must lie between 0 and
79 and if there are two of them, the first may not exceed the second.
80 An atom followed by a bound containing one integer \fIi\fR
82 a sequence of exactly \fIi\fR matches of the atom.
83 An atom followed by a bound
84 containing one integer \fIi\fR and a comma matches
85 a sequence of \fIi\fR or more matches of the atom.
86 An atom followed by a bound
87 containing two integers \fIi\fR and \fIj\fR matches
88 a sequence of \fIi\fR through \fIj\fR (inclusive) matches of the atom.
90 An atom is a regular expression enclosed in "\fI()\fP"
91 (matching a match for the regular expression),
92 an empty set of "\fI()\fP" (matching the null string)\*(dg,
93 a \fIbracket expression\fR (see below), \(aq.\(aq
94 (matching any single character), \(aq\(ha\(aq (matching the null string at the
95 beginning of a line), \(aq$\(aq (matching the null string at the
96 end of a line), a \(aq\e\(aq followed by one of the characters
97 "\fI\(ha.[$()|*+?{\e\fP"
98 (matching that character taken as an ordinary character),
99 a \(aq\e\(aq followed by any other character\*(dg
100 (matching that character taken as an ordinary character,
101 as if the \(aq\e\(aq had not been present\*(dg),
102 or a single character with no other significance (matching that character).
103 A \(aq{\(aq followed by a character other than a digit is an ordinary
104 character, not the beginning of a bound\*(dg.
105 It is illegal to end an RE with \(aq\e\(aq.
107 A \fIbracket expression\fR is a list of characters enclosed in "\fI[]\fP".
108 It normally matches any single character from the list (but see below).
109 If the list begins with \(aq\(ha\(aq,
110 it matches any single character
111 (but see below) \fInot\fR from the rest of the list.
112 If two characters in the list are separated by \(aq\-\(aq, this is shorthand
113 for the full \fIrange\fR of characters between those two (inclusive) in the
115 for example, "\fI[0\-9]\fP" in ASCII matches any decimal digit.
116 It is illegal\*(dg for two ranges to share an
117 endpoint, for example, "\fIa\-c\-e\fP".
118 Ranges are very collating-sequence-dependent,
119 and portable programs should avoid relying on them.
121 To include a literal \(aq]\(aq in the list, make it the first character
122 (following a possible \(aq\(ha\(aq).
123 To include a literal \(aq\-\(aq, make it the first or last character,
124 or the second endpoint of a range.
125 To use a literal \(aq\-\(aq as the first endpoint of a range,
126 enclose it in "\fI[.\fP" and "\fI.]\fP"
127 to make it a collating element (see below).
128 With the exception of these and some combinations using \(aq[\(aq (see next
129 paragraphs), all other special characters, including \(aq\e\(aq, lose their
130 special significance within a bracket expression.
132 Within a bracket expression, a collating element (a character,
133 a multicharacter sequence that collates as if it were a single character,
134 or a collating-sequence name for either)
135 enclosed in "\fI[.\fP" and "\fI.]\fP" stands for the
136 sequence of characters of that collating element.
137 The sequence is a single element of the bracket expression's list.
138 A bracket expression containing a multicharacter collating element
139 can thus match more than one character,
140 for example, if the collating sequence includes a "ch" collating element,
141 then the RE "\fI[[.ch.]]*c\fP" matches the first five characters
144 Within a bracket expression, a collating element enclosed in "\fI[=\fP" and
145 "\fI=]\fP" is an equivalence class, standing for the sequences of characters
146 of all collating elements equivalent to that one, including itself.
147 (If there are no other equivalent collating elements,
148 the treatment is as if the enclosing delimiters
149 were "\fI[.\fP" and "\fI.]\fP".)
150 For example, if o and \o'o\(ha' are the members of an equivalence class,
151 then "\fI[[=o=]]\fP", "\fI[[=\o'o\(ha'=]]\fP",
152 and "\fI[o\o'o\(ha']\fP" are all synonymous.
153 An equivalence class may not\*(dg be an endpoint
156 Within a bracket expression, the name of a \fIcharacter class\fR enclosed
157 in "\fI[:\fP" and "\fI:]\fP" stands for the list
158 of all characters belonging to that
160 Standard character class names are:
172 These stand for the character classes defined in
174 A locale may provide others.
175 A character class may not be used as an endpoint of a range.
176 .\" As per http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=295666
177 .\" The following does not seem to apply in the glibc implementation
179 .\" There are two special cases\*(dg of bracket expressions:
180 .\" the bracket expressions "\fI[[:<:]]\fP" and "\fI[[:>:]]\fP" match
181 .\" the null string at the beginning and end of a word respectively.
182 .\" A word is defined as a sequence of
184 .\" which is neither preceded nor followed by
186 .\" A word character is an
188 .\" character (as defined by
190 .\" or an underscore.
191 .\" This is an extension,
192 .\" compatible with but not specified by POSIX.2,
193 .\" and should be used with
194 .\" caution in software intended to be portable to other systems.
196 In the event that an RE could match more than one substring of a given
198 the RE matches the one starting earliest in the string.
199 If the RE could match more than one substring starting at that point,
200 it matches the longest.
201 Subexpressions also match the longest possible substrings, subject to
202 the constraint that the whole match be as long as possible,
203 with subexpressions starting earlier in the RE taking priority over
205 Note that higher-level subexpressions thus take priority over
206 their lower-level component subexpressions.
208 Match lengths are measured in characters, not collating elements.
209 A null string is considered longer than no match at all.
211 "\fIbb*\fP" matches the three middle characters of "abbbc",
212 "\fI(wee|week)(knights|nights)\fP"
213 matches all ten characters of "weeknights",
214 when "\fI(.*).*\fP" is matched against "abc" the parenthesized subexpression
215 matches all three characters, and
216 when "\fI(a*)*\fP" is matched against "bc"
217 both the whole RE and the parenthesized
218 subexpression match the null string.
220 If case-independent matching is specified,
221 the effect is much as if all case distinctions had vanished from the
223 When an alphabetic that exists in multiple cases appears as an
224 ordinary character outside a bracket expression, it is effectively
225 transformed into a bracket expression containing both cases,
226 for example, \(aqx\(aq becomes "\fI[xX]\fP".
227 When it appears inside a bracket expression, all case counterparts
228 of it are added to the bracket expression, so that, for example, "\fI[x]\fP"
229 becomes "\fI[xX]\fP" and "\fI[\(hax]\fP" becomes "\fI[\(haxX]\fP".
231 No particular limit is imposed on the length of REs\*(dg.
232 Programs intended to be portable should not employ REs longer
234 as an implementation can refuse to accept such REs and remain
237 Obsolete ("basic") regular expressions differ in several respects.
238 \(aq|\(aq, \(aq+\(aq, and \(aq?\(aq are
239 ordinary characters and there is no equivalent
240 for their functionality.
241 The delimiters for bounds are "\fI\e{\fP" and "\fI\e}\fP",
242 with \(aq{\(aq and \(aq}\(aq by themselves ordinary characters.
243 The parentheses for nested subexpressions are "\fI\e(\fP" and "\fI\e)\fP",
244 with \(aq(\(aq and \(aq)\(aq by themselves ordinary characters.
245 \(aq\(ha\(aq is an ordinary character except at the beginning of the
246 RE or\*(dg the beginning of a parenthesized subexpression,
247 \(aq$\(aq is an ordinary character except at the end of the
248 RE or\*(dg the end of a parenthesized subexpression,
249 and \(aq*\(aq is an ordinary character if it appears at the beginning of the
250 RE or the beginning of a parenthesized subexpression
251 (after a possible leading \(aq\(ha\(aq).
253 Finally, there is one new type of atom, a \fIback reference\fR:
254 \(aq\e\(aq followed by a nonzero decimal digit \fId\fR
255 matches the same sequence of characters
256 matched by the \fId\fRth parenthesized subexpression
257 (numbering subexpressions by the positions of their opening parentheses,
259 so that, for example, "\fI\e([bc]\e)\e1\fP" matches "bb" or "cc" but not "bc".
261 Having two kinds of REs is a botch.
263 The current POSIX.2 spec says that \(aq)\(aq is an ordinary character in
264 the absence of an unmatched \(aq(\(aq;
265 this was an unintentional result of a wording error,
266 and change is likely.
269 Back references are a dreadful botch,
270 posing major problems for efficient implementations.
271 They are also somewhat vaguely defined
273 "\fIa\e(\e(b\e)*\e2\e)*d\fP" match "abbbd"?).
276 POSIX.2's specification of case-independent matching is vague.
277 The "one case implies all cases" definition given above
278 is current consensus among implementors as to the right interpretation.
279 .\" As per http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=295666
280 .\" The following does not seem to apply in the glibc implementation
282 .\" The syntax for word boundaries is incredibly ugly.
284 .\" Sigh... The page license means we must have the author's name
285 .\" in the formatted output.
286 This page was taken from Henry Spencer's regex package.
291 POSIX.2, section 2.8 (Regular Expression Notation).