| grep {base} | R Documentation |
Pattern Matching and Replacement
Description
grep searches for matches to pattern (its first
argument) within the vector x of character strings (second argument).
sub and gsub perform replacement of matches
determined by regular expression matching.
The two functions differ only in that sub replaces only
the first occurence of a pattern whereas gsub replaces
all occurences.
The regular expressions used are those specified by POSIX 1003.2,
either extended or basic, depending on the value of the
extended argument.
Usage
grep(pattern, x, ignore.case=FALSE, extended=TRUE, value=FALSE)
sub(pattern, replacement, x,
ignore.case=FALSE, extended=TRUE)
gsub(pattern, replacement, x,
ignore.case=FALSE, extended=TRUE)
Arguments
pattern |
character string containing a regular expression
to be matched in the vector of character string |
x |
a vector of character strings where matches are sought. |
ignore.case |
if |
extended |
if |
value |
if |
replacement |
a replacement for matched pattern in
|
Note
This function is based on the regex regular expression library written by Henry Spencer of the University of Toronto.
See Also
charmatch, pmatch, match.
apropos uses regexps and has nice examples.
Examples
grep("[a-z]", letters)
## Double all 'a' or 'b's; "\" must be escaped, i.e. `doubled'
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) ) # indices
all( txt[i] == grep("[gu]", txt, value = TRUE) )
(ot <- sub("[b-e]",".", txt))
txt[ot != gsub("[b-e]",".", txt)]#- gsub does "global" substitution
txt[gsub("g","#", txt) !=
gsub("g","#", txt, ignore.case = TRUE)] # the "G" words