regex(7)
๐ regex(7) ๐
okay so hi bestie ๐ซถ welcome to regex(7), page 7 of the Miscellaneous Information Manual. yes she's real, yes she's been here forever, and yes she is about to explain regular expressions to you better than literally anyone else. grab a snack. this one's long but it's so worth it.
๐ธ NAME ๐ธ
regex โ POSIX.2 regular expressions.
that's the whole name section. she said "here's my name, that's all you get" ๐
โจ DESCRIPTION โจ
okay so. Regular expressions (everyone calls them "RE"s, keep up) as defined in POSIX.2 come in two forms:
- modern REs โ roughly the ones
egrep(1)uses. POSIX.2 calls these "extended" REs. these are the main character. - obsolete REs โ roughly the ones
ed(1)uses. POSIX.2 calls these "basic" REs.
the obsolete ones mostly exist for backward compatibility in some old programs, bestie, so don't come for them. they get discussed at the very end, so hold that thought.
also important: POSIX.2 leaves some aspects of RE syntax and semantics open. whenever you see (!) in this page, that marks a decision on one of those open aspects โ and it may not be fully portable to other POSIX.2 implementations. so like, vibe check before you ship it, okay?
๐ the modern RE breakdown ๐
A (modern) RE is one (!) or more nonempty (!) branches, separated by '|'. It matches anything that matches one of the branches. (yes, "one or more", that (!) is doing work.)
A branch is one (!) or more pieces, concatenated. It matches a match for the first, followed by a match for the second, and so on. chain them together, slay.
A piece is an atom possibly followed by a single (!) '*', '+', '?', or bound.
- an atom followed by
'*'matches a sequence of 0 or more matches of the atom - an atom followed by
'+'matches a sequence of 1 or more matches of the atom - an atom followed by
'?'matches a sequence of 0 or 1 matches of the atom
๐ฆ bounds ๐ฆ
A bound is '{' followed by an unsigned decimal integer, possibly followed by ',', possibly followed by another unsigned decimal integer, always followed by '}'.
the integers must lie between 0 and RE_DUP_MAX (255(!)) inclusive. and if there are two of them, the first may not exceed the second. rules are rules bestie.
- an atom followed by a bound containing one integer i and no comma matches a sequence of exactly i matches of the atom
- an atom followed by a bound containing one integer i and a comma matches a sequence of i or more matches of the atom
- an atom followed by a bound containing two integers i and j matches a sequence of i through j (inclusive) matches of the atom
๐ what even is an atom ๐
An atom is one of these, and I'm listing all of them because every single one matters:
- a regular expression enclosed in
"()"โ matches a match for the regular expression - an empty set of
"()"โ matches the null string(!) - a bracket expression (see below, we get there)
'.'โ matches any single character'^'โ matches the null string at the beginning of a line'$'โ matches the null string at the end of a line- a
'\'followed by one of the characters"^.[$()|*+?{\"โ matches that character taken as an ordinary character - a
'\'followed by any other character(!)โ matches that character taken as an ordinary character, as if the'\'had not been present(!) - a single character with no other significance โ matches that character
also: a '{' followed by a character other than a digit is an ordinary character, not the beginning of a bound (!).
๐จ and it is illegal to end an RE with '\'. don't do it. she will not forgive you.
๐ bracket expressions ๐
A bracket expression is a list of characters enclosed in "[]". It normally matches any single character from the list (but see below, there's always a but).
- if the list begins with
'^', it matches any single character (but see below) not from the rest of the list - if two characters in the list are separated by
'-', that's shorthand for the full range of characters between those two (inclusive) in the collating sequence. for example,"[0-9]"in ASCII matches any decimal digit - it is illegal
(!)for two ranges to share an endpoint, for example"a-c-e"
ranges are very collating-sequence-dependent, and portable programs should avoid relying on them. no cap.
how to sneak in the tricky characters
- to include a literal
']'in the list, make it the first character (following a possible'^') - to include a literal
'-', make it the first or last character, or the second endpoint of a range - to use a literal
'-'as the first endpoint of a range, enclose it in"[."and".]"to make it a collating element (see below)
with the exception of these and some combinations using '[' (see next paragraphs), all other special characters, including '\', lose their special significance within a bracket expression. they're just vibing in there, ordinary as hell.
๐ง collating elements ๐ง
Within a bracket expression, a collating element โ that's a character, a multicharacter sequence that collates as if it were a single character, or a collating-sequence name for either โ enclosed in "[." and ".]" stands for the sequence of characters of that collating element.
the sequence is a single element of the bracket expression's list. so a bracket expression containing a multicharacter collating element can match more than one character. for example, if the collating sequence includes a "ch" collating element, then the RE "[[.ch.]]*c" matches the first five characters of "chchcc". obsessed with her honestly.
๐ equivalence classes ๐
Within a bracket expression, a collating element enclosed in "[=" and "=]" is an equivalence class, standing for the sequences of characters of all collating elements equivalent to that one, including itself.
(if there are no other equivalent collating elements, the treatment is as if the enclosing delimiters were "[." and ".]".)
for example, if o and รด are the members of an equivalence class, then "[[=o=]]", "[[=รด=]]", and "[oรด]" are all synonymous. same energy, same result.
๐จ an equivalence class may not (!) be an endpoint of a range.
๐ character classes ๐
Within a bracket expression, the name of a character class enclosed in "[:" and ":]" stands for the list of all characters belonging to that class.
the standard character class names are:
alnum digit punct
alpha graph space
blank lower upper
cntrl print xdigit
these stand for the character classes defined in wctype(3). a locale may provide others. and heads up: a character class may not be used as an endpoint of a range.
๐ฅ how matching actually resolves ๐ฅ
in the event that an RE could match more than one substring of a given string, the RE matches the one starting earliest in the string.
if the RE could match more than one substring starting at that point, it matches the longest.
subexpressions also match the longest possible substrings, subject to the constraint that the whole match be as long as possible, with subexpressions starting earlier in the RE taking priority over ones starting later. note that higher-level subexpressions thus take priority over their lower-level component subexpressions. it's giving hierarchy. it's giving main character energy at the top level.
match lengths are measured in characters, not collating elements. and a null string is considered longer than no match at all.
some examples so it clicks:
"bb*"matches the three middle characters of"abbbc""(wee|week)(knights|nights)"matches all ten characters of"weeknights"- when
"(.*).*"is matched against"abc", the parenthesized subexpression matches all three characters - when
"(a*)*"is matched against"bc", both the whole RE and the parenthesized subexpression match the null string
๐ case-independent matching ๐
if case-independent matching is specified, the effect is much as if all case distinctions had vanished from the alphabet.
- when an alphabetic that exists in multiple cases appears as an ordinary character outside a bracket expression, it is effectively transformed into a bracket expression containing both cases. for example,
'x'becomes"[xX]" - when it appears inside a bracket expression, all case counterparts of it are added to the bracket expression. so, for example,
"[x]"becomes"[xX]"and"[^x]"becomes"[^xX]"
โญ how long can an RE be โญ
no particular limit is imposed on the length of REs (!).
but โ programs intended to be portable should not employ REs longer than 256 bytes, because an implementation can refuse to accept such REs and remain POSIX-compliant. so like, 256 bytes, bestie. that's the vibe ceiling.
๐ธ obsolete ("basic") REs ๐ธ
here's the part I promised you at the top. obsolete ("basic") regular expressions differ in several respects:
'|','+', and'?'are ordinary characters, and there is no equivalent for their functionality- the delimiters for bounds are
"\{"and"\}", with'{'and'}'by themselves being ordinary characters - the parentheses for nested subexpressions are
"\("and"\)", with'('and')'by themselves being ordinary characters '^'is an ordinary character except at the beginning of the RE or(!)the beginning of a parenthesized subexpression'$'is an ordinary character except at the end of the RE or(!)the end of a parenthesized subexpression'*'is an ordinary character if it appears at the beginning of the RE or the beginning of a parenthesized subexpression (after a possible leading'^')
๐ back references ๐
finally, there is one new type of atom: a back reference. that's a '\' followed by a nonzero decimal digit d. it matches the same sequence of characters matched by the *d*th parenthesized subexpression (numbering subexpressions by the positions of their opening parentheses, left to right).
so, for example, "\([bc]\)\1" matches "bb" or "cc" but not "bc". cute, right? well. go read the BUGS section. ๐ญ
๐ญ BUGS ๐ญ
okay bestie, deep breaths.
- having two kinds of REs is a botch.
- the current POSIX.2 spec says that
')'is an ordinary character in the absence of an unmatched'('. this was an unintentional result of a wording error, and change is likely. avoid relying on it. - back references are a dreadful botch, posing major problems for efficient implementations. they are also somewhat vaguely defined (does
"a\(\(b\)*\2\)*d"match"abbbd"?). avoid using them. - POSIX.2's specification of case-independent matching is vague. the "one case implies all cases" definition given above is the current consensus among implementors as to the right interpretation.
๐ซถ AUTHOR ๐ซถ
this page was taken from Henry Spencer's regex package. credit where credit is due, king. ๐
๐ SEE ALSO ๐
and that's the whole page, bestie!! you made it to the end, I'm so proud of you ๐ go forth and write regexes that are actually portable. this beauty came from Linux man-pages 6.19, dated 2026-02-08, regex(7). mwah. ๐โจ
๐ฆ where this page comes from
- ubuntu stonking: manpages 6.19-3 ยท GPL-2.0-or-later
- Copyright 1992-1994, Henry Spencer
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms