grepl()
function searchs for matches of a string or string vector. It returns TRUE if a string contains the pattern, otherwise FALSE; if the parameter is a string vector, returns a logical vector (match or not for each element of
the vector). It stands for "grep logical".
grepl(pattern, x, ignore.case = FALSE, perl = FALSE, fixed = FALSE, useBytes = FALSE)
• pattern
: regular expression, or string for fixed=TRUE
• x
: string, the character vector
• perl
: logical. Should perl-compatible regexps be used? Has priority over extended
• 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
> x <- "line 4322: He is now 25 years old, and weights 130lbs" > y <- grepl("\\d+",x) > y
[1] TRUE
> x <- "line 4322: He is now 25 years old, and weights 130lbs" > y <- grepl("[[:digit:]]",x) > y
[1] TRUE
Vector match:
>str <- c("Regular", "expression", "examples of R language") >x <- grepl("x*ress",str) >x
[1] FALSE TRUE FALSE
Regular Expression Syntax: