The grepl() in R is a built-in function that searches for matches of a string or string vector. It accepts a pattern and returns TRUE if a string contains the pattern; otherwise, FALSE. The grepl stands for “grep logical”.
Searching for patterns in strings is important because it lets you locate specific substrings within a larger string or find all occurrences of a pattern within a string.
The functions like grep(), grepl(), regexpr(), gregexpr(), and regexec() searches for matches to argument patterns within every item of a character vector.
If you are using Rstudio and want the grepl() method documentation, type the following command.
?grepl
And you will find all the information about the grepl() method in the below image.
Syntax
grepl(pattern, x, ignore.case = FALSE, perl = FALSE,
fixed = FALSE, useBytes = FALSE)
Parameters
pattern: It is a regular expression or string for fixed=TRUE
x: It is a string, the character vector
perl: Logical. Should Perl-compatible regexps be used? Has priority been overextended?
fixed: It is logical. If the TRUE, the pattern is a string to be matched. Overrides all conflicting arguments
useBytes: It is logical. If TRUE, the matching is done byte-by-byte rather than character-by-character. If you pass useBytes = TRUE, the strings will not be checked before matching, and the real matching will be faster.
Example 1: Searching for simple patterns in character vectors
To find a pattern in a string or string vector, you can use the grepl() function. In their most basic form, regular expressions can see whether a pattern exists inside a character string or a vector of character strings.
rv <- c("KHUSHI", "KRUNAL", "MATE", "AUS")
grepl("K", rv)
Output
[1] TRUE TRUE FALSE FALSE
You can see that it returns a logical vector indicating whether or not a specific element is present. TRUE means it exists, and FALSE means do not exist.
K exists in the first and second elements of the Vector, and that’s why it returns TRUE and otherwise FALSE.
Example 2: Using regular expressions with the grepl() function
sv <- "Infosys is currently trading at 1435 Rs."
op <- grepl("\\d+", sv)
print(op)
Output
[1] TRUE
In this example, we are searching whether the string vector contains a numeric value. It returns TRUE because it has a numeric value.
Passing ignore.case argument to the grepl() function
To ignore the cases of the letters while searching for patterns in R, use the grepl() function and pass the ignore.case = TRUE. It will ignore whether it is in capital or small letters in the string.
rv <- c("khushi", "KRUNAL", "MATE", "AUS")
grepl("K", rv, ignore.case = T)
Output
[1] TRUE TRUE FALSE FALSE
You can see that even though the first element has a small letter k in “khushi”, it still returns TRUE because we explicitly told the R compiler to ignore the case while searching.
If we pass ignore.case = F or FALSE, it will only return the TRUE on the second position.
rv <- c("khushi", "KRUNAL", "MATE", "AUS")
grepl("K", rv, ignore.case = F)
Output
[1] FALSE TRUE FALSE FALSE
Extracting substrings that match a pattern using the grepl() function
To extract substrings that match a pattern using grepl(), follow the below steps.
Step 1: Create a character vector
Create a character vector containing the strings you want to search through and define a pattern you wish to search for.
data <- c("instagram", "facebook", "snapchat", "tiktok")
pattern <- "a"
Step 2: Use the grepl() function
Use the grepl() function to find the elements that match the pattern you are interested in.
data <- c("instagram", "facebook", "snapchat", "tiktok")
pattern <- "a"
matches <- grepl(pattern, data)
Step 3: Subset the original vector and extract the matching elements
You can use the logical vector returned by the grepl() function to subset the original vector and extract the matching elements.
data <- c("instagram", "facebook", "snapchat", "tiktok")
pattern <- "a"
matches <- grepl(pattern, data)
result <- data[matches]
print(result)
Output
[1] "instagram" "facebook" "snapchat"
That’s it.

Krunal Lathiya is a Software Engineer with over eight years of experience. He has developed a strong foundation in computer science principles and a passion for problem-solving. In addition, Krunal has excellent knowledge of Data Science and Machine Learning, and he is an expert in R Language.