Related Articles

Related Articles

Find position of a Matched Pattern in a String in R Programming – grep() Function
  • Last Updated : 03 Jun, 2020

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

# R program to illustrate
# grep functiom
  
# 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)

chevron_right


Output :

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

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# R program to illustrate
# grep functiom
  
# 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)           

chevron_right


Output:

[1] "GFG" "gfg"
[1] "GFG"   "gfg"   "Geeks" "GEEKS"
[1] 3
[1] 4
My Personal Notes arrow_drop_up
Recommended Articles
Page :