Rev 46553 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
% File src/library/base/man/grep.Rd% Part of the R package, http://www.R-project.org% Copyright 1995-2007 R Core Development Team% Distributed under GPL 2 or later\name{grep}\title{Pattern Matching and Replacement}\alias{grep}\alias{sub}\alias{gsub}\alias{regexpr}\alias{gregexpr}\description{\code{grep} searches for matches to \code{pattern} (its firstargument) within the character vector \code{x} (second argument).\code{regexpr} and \code{gregexpr} do too, but return more detail ina different format.\code{sub} and \code{gsub} perform replacement of matches determinedby regular expression matching.}\usage{grep(pattern, x, ignore.case = FALSE, extended = TRUE,perl = FALSE, value = FALSE, fixed = FALSE, useBytes = FALSE)sub(pattern, replacement, x,ignore.case = FALSE, extended = TRUE, perl = FALSE,fixed = FALSE, useBytes = FALSE)gsub(pattern, replacement, x,ignore.case = FALSE, extended = TRUE, perl = FALSE,fixed = FALSE, useBytes = FALSE)regexpr(pattern, text, ignore.case = FALSE, extended = TRUE,perl = FALSE, fixed = FALSE, useBytes = FALSE)gregexpr(pattern, text, ignore.case = FALSE, extended = TRUE,perl = FALSE, fixed = FALSE, useBytes = FALSE)}\arguments{\item{pattern}{character string containing a \link{regular expression}(or character string for \code{fixed = TRUE}) to be matchedin the given character vector. Coerced by\code{\link{as.character}} to a character string if possible.}\item{x, text}{a character vector where matches are sought, or anobject which can be coerced by \code{as.character} to a character vector.}\item{ignore.case}{if \code{FALSE}, the pattern matching is \emph{casesensitive} and if \code{TRUE}, case is ignored during matching.}\item{extended}{if \code{TRUE}, extended regular expression matchingis used, and if \code{FALSE} basic regular expressions are used.}\item{perl}{logical. Should perl-compatible regexps be used?Has priority over \code{extended}.}\item{value}{if \code{FALSE}, a vector containing the (\code{integer})indices of the matches determined by \code{grep} is returned, and if\code{TRUE}, a vector containing the matching elements themselves isreturned.}\item{fixed}{logical. If \code{TRUE}, \code{pattern} is a string to bematched as is. Overrides all conflicting arguments.}\item{useBytes}{logical. If \code{TRUE} the matching is donebyte-by-byte rather than character-by-character. See \sQuote{Details}.}\item{replacement}{a replacement for matched pattern in \code{sub} and\code{gsub}. Coerced to character if possible. For \code{fixed =FALSE} this can include backreferences \code{"\\1"} to\code{"\\9"} to parenthesized subexpressions of \code{pattern}. For\code{perl = TRUE} only, it can also contain \code{"\\U"} or\code{"\\L"} to convert the rest of the replacement to upper orlower case.}}\details{Arguments which should be character strings or character vectors arecoerced to character if possible.The two \code{*sub} functions differ only in that \code{sub} replacesonly the first occurrence of a \code{pattern} whereas \code{gsub}replaces all occurrences.For \code{regexpr} it is an error for \code{pattern} to be \code{NA},otherwise \code{NA} is permitted and gives an \code{NA} match.The regular expressions used are those specified by POSIX 1003.2,either extended or basic, depending on the value of the\code{extended} argument, unless \code{perl = TRUE} when they arethose of PCRE, \url{http://www.pcre.org/}.(The exact set of patterns supported may depend on the version ofPCRE installed on the system in use if \R was configured to use thesystem PCRE.)\code{useBytes} is only used if \code{fixed = TRUE} or \code{perl = TRUE}.Its main effect is to avoid errors/warnings about invalid inputs andspurious matches, but for \code{regexpr} it changes the interpretationof the output.PCRE only supports caseless matching for a non-ASCII pattern in aUTF-8 locale (and not for \code{useBytes = TRUE} in any locale).}\value{For \code{grep} a vector giving either the indices of the elements of\code{x} that yielded a match or, if \code{value} is \code{TRUE}, thematched elements of \code{x} (after coercion, preserving names but noother attributes).For \code{sub} and \code{gsub} a character vector of the same lengthand with the same attributes as \code{x} (after possible coercion).Elements of character vectors \code{x} which are not substituted willbe return unchanged (including any declared encoding). If\code{useBytes = FALSE}, either \code{perl = TRUE} or \code{fixed =TRUE} and any element of \code{pattern}, \code{replacement} and\code{x} is declared to be in UTF-8, the result will be in UTF-8.Otherwise changed elements of the result will be have the encodingdeclared as that of the current locale (see \code{\link{Encoding}} ifthe corresponding input had a declared encoding and the current localeis either Latin-1 or UTF-8.For \code{regexpr} an integer vector of the same length as \code{text}giving the starting position of the first match, or \eqn{-1} if thereis none, with attribute \code{"match.length"} giving the length of thematched text (or \eqn{-1} for no match). In a multi-byte locale thesequantities are in characters rather than bytes unless\code{useBytes = TRUE} is used with \code{fixed = TRUE} or\code{perl = TRUE}.For \code{gregexpr} a list of the same length as \code{text} eachelement of which is an integer vector as in \code{regexpr}, exceptthat the starting positions of every (disjoint) match are given.If in a multi-byte locale the pattern or replacement is not a validsequence of bytes, an error is thrown. An invalid string in \code{x}or \code{text} is a non-match with a warning for \code{grep} or\code{regexpr}, but an error for \code{sub} or \code{gsub}.}\section{Warning}{The standard regular-expression code has been reported to be very slowwhen applied to extremely long character strings(tens of thousands of characters or more): the code used when\code{perl = TRUE} seems much faster and more reliable for suchusages.The standard version of \code{gsub} does not substitute correctlyrepeated word-boundaries (e.g. \code{pattern = "\\b"}).Use \code{perl = TRUE} for such matches.The \code{perl = TRUE} option is only implemented for single-byte andUTF-8 encodings, and will warn if used in a non-UTF-8 multi-bytelocale (unless \code{useBytes = TRUE}).}\references{Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)\emph{The New S Language}.Wadsworth \& Brooks/Cole (\code{grep})}\seealso{\link{regular expression} (aka \code{\link{regexp}}) for the details% the `aka' above is for ESS (and ?reg....) where a space is problematicof the pattern specification.\code{\link{glob2rx}} to turn wildcard matches into regular expressions.\code{\link{agrep}} for approximate matching.\code{\link{tolower}}, \code{\link{toupper}} and \code{\link{chartr}}for character translations.\code{\link{charmatch}}, \code{\link{pmatch}}, \code{\link{match}}.\code{\link{apropos}} uses regexps and has nice examples.}\examples{grep("[a-z]", letters)txt <- c("arm","foot","lefroo", "bafoobar")if(length(i <- grep("foo",txt)))cat("'foo' appears at least once in\n\t",txt,"\n")i # 2 and 4txt[i]## Double all 'a' or 'b's; "\\" must be escaped, i.e., 'doubled'%% and escaped even once more in this *.Rd file!gsub("([ab])", "\\\\1_\\\\1_", "abc and ABC")txt <- c("The", "licenses", "for", "most", "software", "are","designed", "to", "take", "away", "your", "freedom","to", "share", "and", "change", "it.","", "By", "contrast,", "the", "GNU", "General", "Public", "License","is", "intended", "to", "guarantee", "your", "freedom", "to","share", "and", "change", "free", "software", "--","to", "make", "sure", "the", "software", "is","free", "for", "all", "its", "users")( i <- grep("[gu]", txt) ) # indicesstopifnot( txt[i] == grep("[gu]", txt, value = TRUE) )## Note that in locales such as en_US this includes B as the## collation order is aAbBcCdEe ...(ot <- sub("[b-e]",".", txt))txt[ot != gsub("[b-e]",".", txt)]#- gsub does "global" substitutiontxt[gsub("g","#", txt) !=gsub("g","#", txt, ignore.case = TRUE)] # the "G" wordsregexpr("en", txt)gregexpr("e", txt)## trim trailing white spacestr <- 'Now is the time 'sub(' +$', '', str) ## spaces onlysub('[[:space:]]+$', '', str) ## white space, POSIX-stylesub('\\\\s+$', '', str, perl = TRUE) ## Perl-style white space## capitalizinggsub("(\\\\w)(\\\\w*)", "\\\\U\\\\1\\\\L\\\\2", "a test of capitalizing", perl=TRUE)gsub("\\\\b(\\\\w)", "\\\\U\\\\1", "a test of capitalizing", perl=TRUE)}\keyword{character}\keyword{utilities}