grepl()
grepl(pattern, x, ignore.case = FALSE, perl = FALSE, fixed = FALSE, useBytes = FALSE) Returns:
logical · Updated March 13, 2026 · String Functions string pattern-matching regex base
grepl() searches for matches to a pattern in a character vector and returns a logical vector indicating whether each element matches.
Syntax
grepl(pattern, x, ignore.case = FALSE, perl = FALSE,
fixed = FALSE, useBytes = FALSE)
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
| pattern | character | — | A regular expression pattern to match |
| x | character | — | A character vector to search in |
| ignore.case | logical | FALSE | If TRUE, ignore case when matching |
| perl | logical | FALSE | If TRUE, use Perl-compatible regular expressions |
| fixed | logical | FALSE | If TRUE, treat pattern as a literal string (faster) |
| useBytes | logical | FALSE | If TRUE, match byte-by-byte rather than character-by-character |
Examples
Basic usage
fruits <- c("apple", "banana", "cherry", "date", "elderberry")
grepl("a", fruits)
# [1] TRUE TRUE FALSE TRUE FALSE
Case-insensitive matching
colors <- c("Red", "GREEN", "blue", "YELLOW")
# Ignore case when matching
grepl("red", colors, ignore.case = TRUE)
# [1] TRUE TRUE FALSE TRUE
Fixed string matching (faster)
emails <- c("user@example.com", "test@domain.org", "invalid")
grep("@", emails, fixed = TRUE)
# [1] 1 2
Common Patterns
Counting matches
# Count how many elements match
sum(grepl("pattern", strings))
Filtering data frames
# Filter rows containing a pattern
df[grepl("pattern", df$column), ]
Multiple patterns (OR logic)
# Match multiple patterns using alternation
fruits <- c("apple", "orange", "banana", "grape")
grepl("apple|orange", fruits)
# [1] TRUE TRUE FALSE FALSE