grep()
和grepl()
函数介绍
grep()
和grepl()
是用来查找目标字符串或字符串向量中是否包含目标子串。grep()
返回的是整数索引,grepl()
返回的是布尔值索引。
用法
grep(pattern, x, ignore.case = FALSE, perl = FALSE, value = FALSE, fixed = FALSE, useBytes = FALSE, invert = FALSE)
grepl(pattern, x, ignore.case = FALSE, perl = FALSE, fixed = FALSE, useBytes = FALSE)
参数介绍:
pattern
character string containing a regular expression (or character string for fixed = TRUE) to be matched in the given character vector. Coerced by as.character to a character string if possible. If a character vector of length 2 or more is supplied, the first element is used with a warning. Missing values are allowed except for regexpr, gregexpr and regexec.
x, text
a character vector where matches are sought, or an object which can be coerced by as.character to a character vector. Long vectors are supported.
ignore.case
if FALSE, the pattern matching is case sensitive and if TRUE, case is ignored during matching.
perl
logical. Should Perl-compatible regexps be used?
value
if FALSE, a vector containing the (integer) indices of the matches determined by grep is returned, and if TRUE, a vector containing the matching elements themselves is returned.
fixed
logical. If TRUE, pattern is a string to be matched as is. Overrides all conflicting arguments.
useBytes
logical. If TRUE the matching is done byte-by-byte rather than character-by-character. See ‘Details’.
invert
logical. If TRUE return indices or values for elements that do not match.
实例
#创建一个字符串向量
x <- c("d", "a", "c", "abba")
#查找包含a的元素所在的位置
grep("a", x)
# [1] 2 4
#判断每个元素是否包含a,返回的是逻辑向量
grepl("a", x)
# [1] FALSE TRUE FALSE TRUE
#同时匹配多个内容,查找包含a或者c的元素所在的位置
grep("a|c", x)
# [1] 2 3 4
#同时匹配多个内容,判断每个元素是否包含a或者c,返回的是逻辑向量
grepl("a|c", x)
# [1] FALSE TRUE TRUE TRUE
sub()
和gsub()
函数介绍
sub(pattern, replacement, x, ignore.case = FALSE, perl = FALSE,
fixed = FALSE, useBytes = FALSE)
gsub(pattern, replacement, x, ignore.case = FALSE, perl = FALSE,
fixed = FALSE, useBytes = FALSE)
实例
sub(pattern = 'a',replacement = 'QQ',c('a','b','aa'))
# [1] "QQ" "b" "QQa"
> gsub(pattern = 'a',replacement = 'QQ',c('a','b','aa'))
# [1] "QQ" "b" "QQQQ"