Rev 26795 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
\name{regex}\alias{regex}\alias{regexp}\alias{regular expression}\concept{regular expression}\title{Regular Expressions as used in R}\description{This help page documents the regular expression patterns supported by\code{\link{grep}} and related functions \code{regexpr}, \code{sub}and \code{gsub}, as well as by \code{\link{strsplit}}.This is preliminary documentation.}\details{A \sQuote{regular expression} is a pattern that describes a set ofstrings. Three types of regular expressions are used in \R,\emph{extended} regular expressions, used by\code{grep(extended = TRUE)} (its default), \emph{basic} regularexpressions, as used by \code{grep(extended = FALSE)}, and\emph{Perl-like} regular expressions used by \code{grep(perl = TRUE)}.Other functions which use regular expressions (often via the use of\code{grep}) include \code{apropos}, \code{browseEnv},\code{help.search}, \code{list.files}, \code{ls} and \code{strsplit}.These will all use \emph{extended} regular expressions, unless\code{strsplit} is called with argument \code{extended = FALSE}.Patterns are described here as they would be printed by \code{cat}: doremember that backslashes need to be doubled in entering \R characterstrings from the keyboard.}\section{Extended Regular Expressions}{This section covers the regular expressions allowedif \code{extended = TRUE} in \code{grep}, \code{regexpr}, \code{sub},\code{gsub} and \code{strsplit}. They are the GNU implementation ofthe standard POSIX 1003.2.Regular expressions are constructed analogously to arithmeticexpressions, by using various operators to combine smaller expressions.The fundamental building blocks are the regular expressions that matcha single character. Most characters, including all letters anddigits, are regular expressions that match themselves. Anymetacharacter with special meaning may be quoted by preceding it witha backslash.A list of characters enclosed by \code{[} and \code{]}matches any single character in that list; if the first character ofthe list is the caret \code{^}, then it matches any character\emph{not} in the list. For example, the regular expression\code{[0123456789]} matches any single digit, and \code{[^abc]}matches anything except the characters \code{a}, \code{b} or \code{c}.A range of characters may be specified by giving the first and lastcharacters, separated by a hyphen.Certain named classes of characters are predefined, as follows. Theirinterpretation depends on the \emph{locale} (see \link{locales}); theinterpretation below is that of the POSIX locale.\describe{\item{\code{[:alnum:]}}{Alphanumeric characters: \code{[:alpha:]}and \code{[:digit:]}.}\item{\code{[:alpha:]}}{Alphabetic characters: \code{[:lower:]} and\code{[:upper:]}.}\item{\code{[:blank:]}}{Blank characters: space and tab.}\item{\code{[:cntrl:]}}{Control characters. In ASCII, these characters have octal codes000 through 037, and 177 (\code{DEL}). In other character sets,these are the equivalent characters, if any.}\item{\code{[:digit:]}}{Digits: \code{0 1 2 3 4 5 6 7 8 9}.}\item{\code{[:graph:]}}{Graphical characters: \code{[:alnum:]} and\code{[:punct:]}.}\item{\code{[:lower:]}}{Lower-case letters in the current locale.}\item{\code{[:print:]}}{Printable characters: \code{[:alnum:]}, \code{[:punct:]} and space.}\item{\code{[:punct:]}}{Punctuation characters:\code{! " # $ \% & ' ( ) * + , - . / : ; < = > ? @ [ \\ ] ^ _ ` \{ | \} ~}.}%'"` keep Emacs Rd mode happy\item{\code{[:space:]}}{Space characters: tab, newline, vertical tab, form feed, carriagereturn, and space.}\item{\code{[:upper:]}}{Upper-case letters in the current locale.}\item{\code{[:xdigit:]}}{Hexadecimal digits:\code{0 1 2 3 4 5 6 7 8 9 A B C D E F a b c d e f}.}}For example, \code{[[:alnum:]]} means \code{[0-9A-Za-z]}, except thelatter depends upon the locale and the character encoding, whereasthe former is independent of locale and character set. (Note that thebrackets in these class names are part of the symbolic names, and mustbe included in addition to the brackets delimiting the bracket list.)Most metacharacters lose their special meaning inside lists. Toinclude a literal \code{]}, place it first in the list. Similarly, toinclude a literal \code{^}, place it anywhere but first. Finally, toinclude a literal \code{-}, place it first or last. (Only these and\code{\\} remain special.)The period \code{.} matches any single character. The symbol\code{\\w} is a synonym for \code{[[:alnum:]]} and \code{\\W} is asynonym for \code{[^[:alnum]]}.The caret \code{^} and the dollar sign \code{$} are metacharactersthat respectively match the empty string at the beginning and end of aline. The symbols \code{\\<} and \code{\\>} respectively match theempty string at the beginning and end of a word. The symbol \code{\\b}matches the empty string at the edge of a word, and \code{\\B} matchesthe empty string provided it is not at the edge of a word.A regular expression may be followed by one of several repetitionoperators:\describe{\item{\code{?}}{The preceding item is optional and will be matchedat most once.}\item{\code{*}}{The preceding item will be matched zero or moretimes.}\item{\code{+}}{The preceding item will be matched one or moretimes.}\item{\code{{n}}}{The preceding item is matched exactly \code{n}times.}\item{\code{{n,}}}{The preceding item is matched \code{n} or moretimes.}\item{\code{{n,m}}}{The preceding item is matched at least \code{n}times, but not more than \code{m} times.}}Two regular expressions may be concatenated; the resulting regularexpression matches any string formed by concatenating two substringsthat respectively match the concatenated subexpressions.Two regular expressions may be joined by the infix operator \code{|};the resulting regular expression matches any string matching eithersubexpression. For example, \code{abba|cde} matches either thestring \code{abba} or the string \code{cde}.Repetition takes precedence over concatenation, which in turn takesprecedence over alternation. A whole subexpression may be enclosed inparentheses to override these precedence rules.The backreference \code{\\N}, where N is a single digit, matches thesubstring previously matched by the Nth parenthesized subexpression ofthe regular expression.The current code attempts to support traditional usage by assumingthat \code{\{} is not special if it would be the start of an invalidinterval specification. (POSIX allows this behavior as an extension butwe advise users not to rely on it.)}\section{Basic Regular Expressions}{This section covers the regular expressions allowed if\code{extended = FALSE} in \code{grep}, \code{regexpr}, \code{sub}\code{gsub} and \code{strsplit}.In basic regular expressions the metacharacters \code{?}, \code{+},\code{\{}, \code{|}, \code{(}, and \code{)} lose their special meaning;instead use the backslashed versions \code{\\?}, \code{\\+},\code{\\ \{}, \code{\\|}, \code{\\(}, and \code{\\)}.}\section{Perl Regular Expressions}{The \code{perl=TRUE} argument to \code{grep}, \code{regexpr}, \code{sub}and \code{gsub} switches to the PCRE library, that implement regularexpression pattern matching using the same syntax and semantics as Perl5, with just a few differences.A complete description will be added to this help page soon.Meanwhile please consult the man pages for PCRE (especially\code{man pcrepattern} or if that does not exist, \code{man pcre}) onyour system or from the sources at\url{ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/}.If PCRE support was compiled from the sources within \R, the PCREversion is 4.4: PCRE < 4.0 supports less of the Perl regular expressions.}\author{This help page is based on the documentation of GNU grep 2.4.2, fromwhich the C code used by \R has been taken.}\seealso{\code{\link{grep}}, \code{\link{apropos}}, \code{\link{browseEnv}},\code{\link{help.search}}, \code{\link{list.files}},\code{\link{ls}} and \code{\link{strsplit}}.}\keyword{character}