Special Characters in Basic Regular Expressions
Special characters in POSIX Basic Regular Expressions (BREs) have special properties when within certain contexts.
They become ordinary characters (matching themselves) when outside of these contexts, or when escaped with a preceding \
.
Special Characters
Character | Special Property |
---|---|
. | matches any single character |
[ | denotes the start of a bracket expression |
\ | escapes the following special character into an ordinary character |
* | matches the preceding single character/subexpression/back-reference 0+ consecutive times |
^ | limits an entire BRE to match from the beginning of a string |
$ | limits an entire BRE to match to the end of the string |
Contexts
Character(s) | Are Special when... |
---|---|
. [ \ | outside of bracket expressions |
* | outside of bracket expressions, and not first in a BRE/subexpression |
^ | first in a BRE (a left anchor) |
$ | last in a BRE (a right anchor) |
Notes:
All other characters are considered ordinary characters.
Escaping an ordinary character produces undefined results.
Shorthand character classes (
\w
,\s
,\<
,\>
, etc.) are GNU extensions and are not part of the POSIX Regular Expression specification.
The Open Group Base Specifications Issue 7 (9. Regular Expressions), GNU sed regular expression extensions