Regular Expressions/POSIX Basic Regular Expressions
The POSIX Basic Regular Expression syntax provided extensions to achieve consistency between utility programs such as grep, sed and awk. These extensions are not supported by some traditional implementations of Unix tools.
Traditional Unix regular expression syntax followed common conventions that often differed from tool to tool. The POSIX Basic Regular Expressions syntax was developed by the IEEE, together with an extended variant called Extended Regular Expression syntax. These standards were designed mostly to provide backward compatibility with the traditional Simple Regular Expressions syntax, providing a common standard which has since been adopted as the default syntax of many Unix regular expression tools.
In POSIX Basic Regular Expression syntax, most characters are treated as literals — they match only themselves (e.g.,
a matches "a"). The exceptions, listed below, are called metacharacters or metasequences.
||Matches any single character (many applications exclude newlines, and exactly which characters are considered newlines is flavor, character encoding, and platform specific, but it is safe to assume that the line feed character is included). Within POSIX bracket expressions, the dot character matches a literal dot. For example,
||A bracket expression. Matches a single character that is contained within the brackets. For example,
||Matches a single character that is not contained within the brackets. For example,
||Matches the starting position within the string. In line-based tools, it matches the starting position of any line.|
||Matches the ending position of the string or the position just before a string-ending newline. In line-based tools, it matches the ending position of any line.|
|Defines a marked subexpression. The string matched within the parentheses can be recalled later (see the next entry,
||Matches what the nth marked subexpression matched, where n is a digit from 1 to 9. This construct is theoretically irregular and was not adopted in the POSIX ERE syntax. Some tools allow referencing more than nine capturing groups.|
||Matches the preceding element zero or more times. For example,
|Matches the preceding element at least m and not more than n times. For example,
.atmatches any three-character string ending with "at", including "hat", "cat", and "bat".
[hc]atmatches "hat" and "cat".
[^b]atmatches all strings matched by
^[hc]atmatches "hat" and "cat", but only at the beginning of the string or line.
[hc]at$matches "hat" and "cat", but only at the end of the string or line.
\[.\]matches any single character surrounded by "[" and "]" since the brackets are escaped, for example: "[a]" and "[b]".
The POSIX standard defines some classes or categories of characters as shown in the following table:
|POSIX class||similar to||meaning|
|[:alpha:]||[A-Za-z]||upper- and lowercase letters|
|[:alnum:]||[A-Za-z0-9]||digits, upper- and lowercase letters|
|[:blank:]||[ \t]||space and TAB characters only|
|[:space:]||[ \t\n\r\f\v]||blank (whitespace) characters|
|[:graph:]||[^ \t\n\r\f\v]||printed characters|
|[:print:]||[^\t\n\r\f\v]||printed characters and space|
Use in Tools
Tools and languages that utilize this regular expression syntax include: