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

ParameterTypeDefaultDescription
patterncharacterA regular expression pattern to match
xcharacterA character vector to search in
ignore.caselogicalFALSEIf TRUE, ignore case when matching
perllogicalFALSEIf TRUE, use Perl-compatible regular expressions
fixedlogicalFALSEIf TRUE, treat pattern as a literal string (faster)
useByteslogicalFALSEIf 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

See Also