{
    "mode": "man",
    "parameter": "REGEX",
    "section": "7",
    "url": "https://www.chedong.com/phpMan.php/man/REGEX/7/json",
    "generated": "2026-06-10T15:49:04Z",
    "sections": {
        "NAME": {
            "content": "regex - POSIX.2 regular expressions\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "Regular  expressions  (\"RE\"s),  as defined in POSIX.2, come in two forms: modern REs (roughly\nthose of egrep; POSIX.2 calls these \"extended\" REs) and obsolete REs (roughly those of ed(1);\nPOSIX.2  \"basic\" REs).  Obsolete REs mostly exist for backward compatibility in some old pro‐\ngrams; they will be discussed at the end.  POSIX.2 leaves some aspects of RE syntax  and  se‐\nmantics  open; \"(!)\" marks decisions on these aspects that may not be fully portable to other\nPOSIX.2 implementations.\n\nA (modern) RE is one(!) or more nonempty(!) branches, separated by '|'.  It matches  anything\nthat matches one of the branches.\n\nA  branch is one(!) or more pieces, concatenated.  It matches a match for the first, followed\nby a match for the second, and so on.\n\nA piece is an atom possibly followed by a single(!) '*', '+', '?', or bound.   An  atom  fol‐\nlowed  by  '*'  matches a sequence of 0 or more matches of the atom.  An atom followed by '+'\nmatches a sequence of 1 or more matches of the atom.  An atom followed by '?' matches  a  se‐\nquence of 0 or 1 matches of the atom.\n\nA  bound  is  '{'  followed by an unsigned decimal integer, possibly followed by ',' possibly\nfollowed by another unsigned decimal integer, always followed by '}'.  The integers must  lie\nbetween  0 and REDUPMAX (255(!)) inclusive, and if there are two of them, the first may not\nexceed the second.  An atom followed by a bound containing one integer i and no comma matches\na  sequence of exactly i matches of the atom.  An atom followed by a bound containing one in‐\nteger i and a comma matches a sequence of i or more matches of the atom.  An atom followed by\na bound containing two integers i and j matches a sequence of i through j (inclusive) matches\nof the atom.\n\nAn atom is a regular expression enclosed in \"()\" (matching a match for  the  regular  expres‐\nsion),  an empty set of \"()\" (matching the null string)(!), a bracket expression (see below),\n'.' (matching any single character), '^' (matching the null string  at  the  beginning  of  a\nline),  '$'  (matching  the  null  string at the end of a line), a '\\' followed by one of the\ncharacters \"^.[$()|*+?{\\\" (matching that character taken as an  ordinary  character),  a  '\\'\nfollowed  by any other character(!)  (matching that character taken as an ordinary character,\nas if the '\\' had not been present(!)), or a single  character  with  no  other  significance\n(matching  that  character).  A '{' followed by a character other than a digit is an ordinary\ncharacter, not the beginning of a bound(!).  It is illegal to end an RE with '\\'.\n\nA bracket expression is a list of characters enclosed in \"[]\".  It normally matches any  sin‐\ngle  character  from  the  list (but see below).  If the list begins with '^', it matches any\nsingle character (but see below) not from the rest of the list.  If  two  characters  in  the\nlist  are  separated by '-', this is shorthand for the full range of characters between those\ntwo (inclusive) in the collating sequence, for example, \"[0-9]\" in ASCII matches any  decimal\ndigit.   It  is illegal(!) for two ranges to share an endpoint, for example, \"a-c-e\".  Ranges\nare very collating-sequence-dependent, and portable programs should avoid relying on them.\n\nTo include a literal ']' in the list, make it the first character (following a possible '^').\nTo  include  a  literal '-', make it the first or last character, or the second endpoint of a\nrange.  To use a literal '-' as the first endpoint of a range, enclose it in  \"[.\"  and  \".]\"\nto  make  it  a collating element (see below).  With the exception of these and some combina‐\ntions using '[' (see next paragraphs), all other  special  characters,  including  '\\',  lose\ntheir special significance within a bracket expression.\n\nWithin a bracket expression, a collating element (a character, a multicharacter sequence that\ncollates as if it were a single character, or a collating-sequence name for either)  enclosed\nin  \"[.\"  and  \".]\" stands for the sequence of characters of that collating element.  The se‐\nquence is a single element of the bracket expression's list.  A bracket expression containing\na  multicharacter  collating  element can thus match more than one character, for example, if\nthe collating sequence includes a \"ch\" collating element, then the  RE  \"[[.ch.]]*c\"  matches\nthe first five characters of \"chchcc\".\n\nWithin  a bracket expression, a collating element enclosed in \"[=\" and \"=]\" is an equivalence\nclass, standing for the sequences of characters of all collating elements equivalent to  that\none,  including  itself.  (If there are no other equivalent collating elements, the treatment\nis as if the enclosing delimiters were \"[.\" and \".]\".)  For example, if o and ^ are the  mem‐\nbers  of  an equivalence class, then \"[[=o=]]\", \"[[==]]\", and \"[o]\" are all synonymous.  An\nequivalence class may not(!) be an endpoint of a range.\n\nWithin a bracket expression, the name of a character class enclosed in \"[:\" and  \":]\"  stands\nfor the list of all characters belonging to that class.  Standard character class names are:\n\nalnum   digit   punct\nalpha   graph   space\nblank   lower   upper\ncntrl   print   xdigit\n\nThese  stand for the character classes defined in wctype(3).  A locale may provide others.  A\ncharacter class may not be used as an endpoint of a range.\n\nIn the event that an RE could match more than one substring of a given string, the RE matches\nthe  one  starting  earliest  in  the  string.  If the RE could match more than one substring\nstarting at that point, it matches the longest.  Subexpressions also match the longest possi‐\nble  substrings,  subject to the constraint that the whole match be as long as possible, with\nsubexpressions starting earlier in the RE taking priority over  ones  starting  later.   Note\nthat  higher-level  subexpressions thus take priority over their lower-level component subex‐\npressions.\n\nMatch lengths are measured in characters, not collating elements.  A null string  is  consid‐\nered  longer than no match at all.  For example, \"bb*\" matches the three middle characters of\n\"abbbc\", \"(wee|week)(knights|nights)\"  matches  all  ten  characters  of  \"weeknights\",  when\n\"(.*).*\"  is  matched against \"abc\" the parenthesized subexpression matches all three charac‐\nters, and when \"(a*)*\" is matched against \"bc\" both the whole RE and the parenthesized subex‐\npression match the null string.\n\nIf case-independent matching is specified, the effect is much as if all case distinctions had\nvanished from the alphabet.  When an alphabetic that exists in multiple cases appears  as  an\nordinary character outside a bracket expression, it is effectively transformed into a bracket\nexpression containing both cases, for example, 'x' becomes \"[xX]\".  When it appears inside  a\nbracket expression, all case counterparts of it are added to the bracket expression, so that,\nfor example, \"[x]\" becomes \"[xX]\" and \"[^x]\" becomes \"[^xX]\".\n\nNo particular limit is imposed on the length of REs(!).  Programs  intended  to  be  portable\nshould  not  employ REs longer than 256 bytes, as an implementation can refuse to accept such\nREs and remain POSIX-compliant.\n\nObsolete (\"basic\") regular expressions differ in several respects.  '|', '+', and '?' are or‐\ndinary  characters  and  there  is no equivalent for their functionality.  The delimiters for\nbounds are \"\\{\" and \"\\}\", with '{' and '}' by themselves ordinary characters.  The  parenthe‐\nses  for  nested  subexpressions  are  \"\\(\" and \"\\)\", with '(' and ')' by themselves ordinary\ncharacters.  '^' is an ordinary character except at the beginning of the RE or(!) the  begin‐\nning  of a parenthesized subexpression, '$' is an ordinary character except at the end of the\nRE or(!) the end of a parenthesized subexpression, and '*' is an ordinary character if it ap‐\npears  at  the beginning of the RE or the beginning of a parenthesized subexpression (after a\npossible leading '^').\n\nFinally, there is one new type of atom, a back reference: '\\' followed by a  nonzero  decimal\ndigit  d  matches the same sequence of characters matched by the dth parenthesized subexpres‐\nsion (numbering subexpressions by the positions of their opening parentheses, left to right),\nso that, for example, \"\\([bc]\\)\\1\" matches \"bb\" or \"cc\" but not \"bc\".\n",
            "subsections": []
        },
        "BUGS": {
            "content": "Having two kinds of REs is a botch.\n\nThe  current  POSIX.2  spec  says  that ')' is an ordinary character in the absence of an un‐\nmatched '('; this was an unintentional result of a  wording  error,  and  change  is  likely.\nAvoid relying on it.\n\nBack  references  are  a dreadful botch, posing major problems for efficient implementations.\nThey are also somewhat vaguely defined (does \"a\\(\\(b\\)*\\2\\)*d\" match \"abbbd\"?).  Avoid  using\nthem.\n\nPOSIX.2's  specification  of  case-independent  matching is vague.  The \"one case implies all\ncases\" definition given above is current consensus among implementors as to the right  inter‐\npretation.\n",
            "subsections": []
        },
        "AUTHOR": {
            "content": "This page was taken from Henry Spencer's regex package.\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "grep(1), regex(3)\n\nPOSIX.2, section 2.8 (Regular Expression Notation).\n",
            "subsections": []
        },
        "COLOPHON": {
            "content": "This  page  is  part  of  release  5.10 of the Linux man-pages project.  A description of the\nproject, information about reporting bugs, and the latest version of this page, can be  found\nat https://www.kernel.org/doc/man-pages/.\n\n\n\n2020-08-13                                     REGEX(7)",
            "subsections": []
        }
    },
    "summary": "regex - POSIX.2 regular expressions",
    "flags": [],
    "examples": [],
    "see_also": [
        {
            "name": "grep",
            "section": "1",
            "url": "https://www.chedong.com/phpMan.php/man/grep/1/json"
        },
        {
            "name": "regex",
            "section": "3",
            "url": "https://www.chedong.com/phpMan.php/man/regex/3/json"
        }
    ]
}