Open In App

Find position of a Matched Pattern in a String in R Programming – grep() Function

Last Updated : 12 Apr, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

grep() function in R Language is used to search for matches of a pattern within each element of the given string.
 

Syntax: 
grep(pattern, x, ignore.case=TRUE/FALSE, value=TRUE/FALSE)
Parameters: 
pattern: Specified pattern which is going to be matched with given elements of the string. 
x: Specified string vector. 
ignore.case: If its value is TRUE, it ignores case. 
value: If its value is TRUE, it return the matching elements vector, else return the indices vector. 
 


Example 1: 
 

Python3
# R program to illustrate
# grep function

# Creating string vector
x <- c("GFG", "gfg", "Geeks", "GEEKS")

# Calling grep() function
grep("gfg", x)
grep("Geeks", x)
grep("gfg", x, ignore.case = FALSE)
grep("Geeks", x, ignore.case = TRUE)

Output : 
 

[1] 2
[1] 3
[1] 2
[1] 3 4


Example 2: 
 

Python3
# R program to illustrate
# grep function

# Creating string vector
x <- c("GFG", "gfg", "Geeks", "GEEKS")

# Calling grep() function
grep("gfg", x, ignore.case = TRUE, value = TRUE)
grep("G", x, ignore.case = TRUE, value = TRUE)
grep("Geeks", x, ignore.case = FALSE, value = FALSE)
grep("GEEKS", x, ignore.case = FALSE, value = FALSE)           

Output: 
 

[1] "GFG" "gfg"
[1] "GFG"   "gfg"   "Geeks" "GEEKS"
[1] 3
[1] 4


 


Next Article

Similar Reads