regex(7)

๐Ÿ’– regex(7) ๐Ÿ’–

hiii bestie ๐Ÿซถ you've landed on regex(7), straight outta the Miscellaneous Information Manual, and honestly? this page is the moment. it's all about POSIX.2 regular expressions โ€” the little patterns that make everything else go brrr. grab a strawberry ๐Ÿ“ and let's get into it.

โœจ NAME โœจ

regex โ€” POSIX.2 regular expressions

๐ŸŒˆ DESCRIPTION ๐ŸŒˆ

Okay so first thing, no cap: regular expressions ("RE"s), 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 what ed(1) uses. POSIX.2 calls these "basic" REs. They mostly exist for backward compatibility in some old programs, and we'll get to them at the very end. ๐Ÿ™ˆ

Also, POSIX.2 leaves some aspects of RE syntax and semantics open, so wherever that happens you'll see a little "(!)" โ€” that's the flag meaning "this is a decision that may not be fully portable to other POSIX.2 implementations." iykyk. ๐ŸŽ€

A (modern) RE is one(!) or more nonempty(!) branches, separated by |. It matches anything that matches one of the branches. periodt.

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.

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.

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.

  • 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.

An atom is one of these little guys:

  • a regular expression enclosed in () (matching a match for the regular expression)
  • an empty set of () (matching the null string)(!)
  • a bracket expression (see below ๐Ÿ‘€)
  • . (matching any single character)
  • ^ (matching the null string at the beginning of a line)
  • $ (matching the null string at the end of a line)
  • a \ followed by one of the characters "^.[$()|*+?{\ (matching that character taken as an ordinary character)
  • a \ followed by any other character(!) (matching that character taken as an ordinary character, as if the \ had not been present(!))
  • or a single character with no other significance (matching that character)

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 \. ๐Ÿ’€


A bracket expression is a list of characters enclosed in []. It normally matches any single character from the list (but see below ๐Ÿ‘€). 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. fr fr.

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. ๐Ÿซถ

Within a bracket expression, a collating element (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. That sequence is a single element of the bracket expression's list. A bracket expression containing a multicharacter collating element can thus 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. slay โœจ

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. An equivalence class may not(!) be an endpoint of a range.

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 a character class may not be used as an endpoint of a range. ๐Ÿ’…


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. main character energy, honestly. ๐Ÿ‘‘

Match lengths are measured in characters, not collating elements. A null string is considered longer than no match at all. For example:

  • 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

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 that, for example, [x] becomes [xX] and [^x] becomes [^xX].

No particular limit is imposed on the length of REs(!). But programs intended to be portable should not employ REs longer than 256 bytes, since an implementation can refuse to accept such REs and remain POSIX-compliant. lowkey a warning, bestie. ๐Ÿšฉ


Okay now the โœจvintageโœจ section. 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 ordinary characters.
  • The parentheses for nested subexpressions are \( and \), with ( and ) by themselves 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 ^).

And finally โ€” plot twist โ€” there's one new type of atom: a back reference. A \ followed by a nonzero decimal digit d 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. the way that works is so unserious ๐Ÿ˜ญ

๐Ÿ’€ BUGS ๐Ÿ’€

  • 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 current consensus among implementors as to the right interpretation.

๐Ÿซถ AUTHOR ๐Ÿซถ

This page was taken from Henry Spencer's regex package.

๐Ÿ‘‘ SEE ALSO ๐Ÿ‘‘


and that's the whole page, bestie ๐ŸŒธ this one shipped with Linux man-pages 6.20, dated 2026-02-08. go forth and match responsibly ๐Ÿ’–โœจ๐Ÿฆ‹

๐Ÿ“ฆ where this page comes from
  • arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms