We often come across a functionality where we want to check something by comparing it with patten. We use a regular expression to match specific character vectors in the string. Let’s see how to use regular expression in R.
The functions like grep(), grepl(), regexpr(), gregexpr(), and regexec() search for matches to argument pattern within every item of a character vector. The sub() and gsub() functions execute the replacement of the first and all matches sequentially.
If you are using Rstudio and you want the documentation of the grepl() method, then type the following command.
And you will find all the information about the grepl() method like the below image.
grepl in R
The grepl in R is a built-in function that searches for matches of a string or string vector. The grepl() method takes a pattern and returns TRUE if a string contains the pattern, otherwise FALSE. The grepl stands for “grep logical”.
If the parameter is a string vector, it returns a logical vector (match or not for each vector element).
To find a pattern in string or string vector, 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.
grepl(pattern, x, ignore.case = FALSE, perl = FALSE, fixed = FALSE, useBytes = FALSE)
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 as is. 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.
Both functions need a pattern and an x argument, where the pattern is the regular expression you want to match for, and the x argument is the character vector from which matches should be sought. If you are performing a lot of regular expression matching, including on very long strings, you might want to consider the options used.
Let’s create a vector consists of strings.
rv <- c("KHUSHI", "KRUNAL", "MATE", "AUS") grepl("K", rv)
 TRUE TRUE FALSE FALSE
You can see that it returns a logical vector indicating that if a specific element is present or not. 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.
sv <- "Infosys is currently trading at 1435 Rs." op <- grepl("\\d+", sv) print(op)
In this example, we are searching if the string vector contains a numeric value or not. 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)
 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)
 FALSE TRUE FALSE FALSE
grep in R
The grep() is a built-in R function that returns the index of the match. Take the above example and find the index of matched elements.
rv <- c("KHUSHI", "KRUNAL", "MATE", "AUS") grep("K", rv)
 1 2
You can see that K exists in the 1st and 2nd items of the Vector; that’s why it returns the indices of that Vector, which are 1 and 2.
That is it for this tutorial.
Krunal Lathiya is an Information Technology Engineer by education and web developer by profession. He has worked with many back-end platforms, including Node.js, PHP, and Python. In addition, Krunal has excellent knowledge of Data Science and Machine Learning, and he is an expert in R Language. Krunal has written many programming blogs, which showcases his vast expertise in this field.