Regular Expressions/POSIX Basic Regular Expressions

From Wikibooks, open books for an open world
Jump to: navigation, search

The POSIX Basic Regular Expression (BRE) 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.

History[edit]

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.

Syntax[edit]

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.

Metacharacter Description
. 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.c matches "abc", etc., but [a.c] matches only "a", ".", or "c".
[ ] A bracket expression. Matches a single character that is contained within the brackets. For example, [abc] matches "a", "b", or "c", and [a-z] specifies a range which matches any lowercase letter from "a" to "z". These forms can be mixed: [abcx-z] matches "a", "b", "c", "x", "y", or "z", as does [a-cx-z].

The - character is treated as a literal character if it is the last or the first character within the brackets: [abc-], [-abc]. The ] character can be included in a bracket expression if it is the first character: []abc]. The bracket expression may also contain character classes, equivalence classes, and collating characters.

[^ ] Matches a single character that is not contained within the brackets. For example, [^abc] matches any character other than "a", "b", or "c", and [^a-z] matches any single character that is not a lowercase letter from "a" to "z". These forms can be mixed: [^abcx-z] matches any character other than "a", "b", "c", "x", "y", or "z".

The - character is treated as a literal character if it is the last character or the first characted after ^: [^abc-], [^-abc]. The ] character is treated as a literal character if it is the first character after ^: [^]abc]. The expression may also contain character classes, equivalence classes, and collating characters.

^ Matches the starting position within the string, if it is the first character of the regular expression.
$ Matches the ending position of the string, if it is the last character of the regular expression.
* Matches the preceding element zero or more times. For example, ab*c matches "ac", "abc", "abbbc", etc. [xyz]* matches "", "x", "y", "z", "zx", "zyx", "xyzzy", and so on.
BRE: \{m\}
ERE: {m}
Matches the preceding element exactly m times. For example, a\{3\} matches only "aaa".
BRE: \{m,\}
ERE: {m,}
Matches the preceding element at least m times. For example, a\{3,\} matches "aaa", "aaaa", "aaaaa", "aaaaaa", "aaaaaaa", and so on.
BRE: \{m,n\}
ERE: {m,n}
Matches the preceding element at least m and not more than n times. For example, a\{3,5\} matches only "aaa", "aaaa", and "aaaaa". This is not found in a few older instances of regular expressions.
BRE: \( \)
ERE: ( )
Defines a subexpression. It is treated as a single element. For example, ab* matches "a", "ab", "abb" and so on, while \(ab\)* matches "", "ab", "abab", "ababab", and so on. The string matched within the parentheses can be recalled later (see the next entry, \n). A subexpression is also called a marked subexpression, a block or a capturing group.
BRE only: \n Matches what the nth marked subexpression matched, where n is a digit from 1 to 9. This construct is theoretically irregular (an expression with this construct does not obey the mathematical definition of regular expression), and was not adopted in the POSIX ERE syntax.

Examples:

  • .at matches any three-character string ending with "at", including "hat", "cat", and "bat".
  • [hc]at matches "hat" and "cat".
  • [^b]at matches all strings matched by .at except "bat".
  • ^[hc]at matches "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]".

Character classes[edit]

The POSIX standard defines some classes or categories of characters as shown below. These classes are used within brackets.

POSIX class similar to meaning
[:upper:] [A-Z] uppercase letters
[:lower:] [a-z] lowercase letters
[:alpha:] [A-Za-z] upper- and lowercase letters
[:digit:] [0-9] digits
[:xdigit:] [0-9A-Fa-f] hexadecimal digits
[:alnum:] [A-Za-z0-9] digits, upper- and lowercase letters
[:punct:] punctuation (all graphic characters except letters and digits)
[:blank:] [ \t] space and TAB characters only
[:space:] [ \t\n\r\f\v] blank (whitespace) characters
[:cntrl:] control characters
[:graph:] [^ [:cntrl:]] graphic characters (all characters which have graphic representation)
[:print:] [[:graph] ] graphic characters and space

For example,

  • a[[:digit:]]b matches "a0b", "a1b", ..., "a9b".
  • a[:digit:]b is an error: character classes must be in brackets
  • [[:digit:]abc] matches any digit, "a", "b", and "c".
  • [abc[:digit:]] is the same as above
  • [^ABZ[:lower:]] matches any character except lowercase letters, A, B, and Z.

Collating symbols[edit]

Collating symbols, like character classes, are used in brackets and have the form [.ch.]. Here ch is a digraph. Collating systems are defined by the locale.

Equivalence classes[edit]

Equivalence classes, like character classes and collating symbols, are used in brackets and have the form [=a=]. They stand for any character which is equivalent to the given. According to the standard[1],

For example, if 'a', 'à', and 'â' belong to the same equivalence class, then "[[=a=]b]", "[[=à=]b]", and "[[=â=]b]" are each equivalent to "[aàâb]".

Equivalence classes, like collating symbols, are defined by the locale.

External links[edit]

Use in Tools[edit]

Tools and languages that utilize this regular expression syntax include:

Links[edit]