REGEX(7) | Standards, Environments, and Macros | REGEX(7) |
regex
—
The Basic Regular Expression (BRE) notation and construction rules described in the BASIC REGULAR EXPRESSIONS section apply to most utilities supporting regular expressions. Some utilities, instead, support the Extended Regular Expressions (ERE) described in the EXTENDED REGULAR EXPRESSIONS section; any exceptions for both cases are noted in the descriptions of the specific utilities using regular expressions. Both BREs and EREs are supported by the Regular Expression Matching interfaces regcomp(3C) and regexec(3C).
The interpretation of an ordinary character preceded by a backslash (“\”) is undefined, except for:
The following rules and definitions apply to bracket expressions:
The special characters “.”, “*”, “[”, “\” (period, asterisk, left-bracket and backslash, respectively) lose their special meaning within a bracket expression.
The character sequences “[.”, “[=”, “[:” (left-bracket followed by a period, equals-sign, or colon) are special inside a bracket expression and are used to delimit collating symbols, equivalence class expressions, and character class expressions. These symbols must be followed by a valid expression and the matching terminating sequence “.]”, “=]” or “:]”, as described in the following items.
LC_CTYPE
category in the current locale. All
character classes specified in the current locale will be recognized. A
character class expression is expressed as a character class name enclosed
within bracket-colon (“[::]”) delimiters.
The following character class expressions are supported in all locales:
[:alnum:] | [:cntrl:] | [:lower:] | [:space:] |
[:alpha:] | [:digit:] | [:print:] | [:upper:] |
[:blank:] | [:graph:] | [:punct:] | [:xdigit:] |
In addition, character class expressions of the form
“[:name:]” are recognized in those locales where the
name keyword has been given a
charclass definition in the
LC_CTYPE
category.
Range expressions must not be used in portable applications because their behavior is dependent on the collating sequence. Ranges will be treated according to the current collating sequence, and include such characters that fall within the range based on that collating sequence, regardless of character values. This, however, means that the interpretation will differ depending on collating sequence. If, for instance, one collating sequence defines as a variant of “a”, while another defines it as a letter following “z”, then the expression “[-z]” is valid in the first language and invalid in the second.
In the following, all examples assume the collation sequence specified for the POSIX locale, unless another collation sequence is specifically defined.
The starting range point and the ending range point must be a collating element or collating symbol. An equivalence class expression used as a starting or ending point of a range expression produces unspecified results. An equivalence class can be used portably within a bracket expression, but only outside the range. For example, the unspecified expression “[[=e=]-f]” should be given as “[[=e=]e-f]”. The ending range point must collate equal to or higher than the starting range point; otherwise, the expression will be treated as invalid. The order used is the order in which the collating elements are specified in the current collation definition. One-to-many mappings (see locale(7)) will not be performed. For example, assuming that the character “eszet” is placed in the collation sequence after “r” and “s”, but before “t”, and that it maps to the sequence “ss” for collation purposes, then the expression “[r-s]” matches only “r” and “s”, but the expression “[s-t]” matches “s”, “beta”, or “t”.
The interpretation of range expressions where the ending range point is also the starting range point of a subsequent range expression (for instance “[a-m-o]”) is undefined.
The hyphen character will be treated as itself if it occurs first (after an initial “^”, if any) or last in the list, or as an ending range point in a range expression. As examples, the expressions “[-ac]” and “[ac-]” are equivalent and match any of the characters “a”, “c”, or “-;” “[^-ac]” and “[^ac-]” are equivalent and match any characters except “a”, “c”, or “-;” the expression “[%--]” matches any of the characters between “%” and “-” inclusive; the expression “[--@]” matches any of the characters between “-” and “@” inclusive; and the expression “[a--@]” is invalid, because the letter “a” follows the symbol “-” in the POSIX locale. To use a hyphen as the starting range point, it must either come first in the bracket expression or be specified as a collating symbol, for example: “[][.-.]-0]”, which matches either a right bracket or any character or collating element that collates between hyphen and 0, inclusive.
If a bracket expression must specify both “-” and “]”, the “]” must be placed first (after the “^”, if any) and the “-” last within the bracket expression.
Note: Latin-1 characters such as “`” or “^” are not printable in some locales, for example, the ja locale.
BRE_DUP_MAX
, where m specifies
the exact or minimum number of occurrences and n
specifies the maximum number of occurrences. The expression
“\{m\}” matches exactly
m occurrences of the preceding BRE,
“\{m,\}” matches at least
m occurrences and
“\{m,n\}” matches any
number of occurrences between m and n,
inclusive.
For example, in the string “abababccccccd”, the BRE “c\{3\}” is matched by characters seven to nine, the BRE “\(ab\)\{4,\}” is not matched at all and the BRE “c\{1,3\}d” is matched by characters ten to thirteen.
The behavior of multiple adjacent duplication symbols (“*” and intervals) produces undefined results.
BRE Precedence (from high to low) | |
collation-related bracket symbols | [= =] [: :] [. .] |
escaped characters | \<special character> |
bracket expression | [] |
subexpressions/back-references | \( \) \n |
single-character-BRE duplication | * \{m,n\} |
concatenation | |
anchoring | ^ $ |
Note: The Solaris implementation does not support anchoring in BRE subexpressions.
RE_DUP_MAX
, where m specifies
the exact or minimum number of occurrences and n
specifies the maximum number of occurrences. The expression
“{m}” matches exactly m
occurrences of the preceding ERE, “{m,}”
matches at least m occurrences and
“{m,n}” matches any number of occurrences
between m and n, inclusive.For example, in the string “abababccccccd” the ERE “c{3}” is matched by characters seven to nine and the ERE “(ab){2,}” is matched by characters one to six.
The behavior of multiple adjacent duplication symbols (“+”, “*”, “?” and intervals) produces undefined results.
ERE Precedence (from high to low) | |
collation-related bracket symbols | [= =] [: :] [. .] |
escaped characters | \<special character> |
bracket expression | [ ] |
grouping | ( ) |
single-character-ERE duplication | * + ? {m,n Ns} |
concatenation | |
anchoring | ^ $ |
alternation | | |
For example, the ERE “abba|cde” matches either the string “abba” or the string “cde” (rather than the string “abbade” or “abbcde”, because concatenation has a higher order of precedence than alternation).
August 14, 2020 | OmniOS |