Open In App

Seek a Match for the Pattern in the String in R Programming – pmatch() Function

pmatch() function in R Language is used to seek match for the pattern passed as argument. It returns the strings that begin with the pattern being searched.

Syntax: pmatch(pat, string, nomatch)



Parameters:
pat: pattern vector to be searched
string: vector to be matched
nomatch: return when no match is found

Example 1:






# R Program to match pattern in a string
  
# Creating string vector
x <- c("Geeks", "Books", "geek")
x
  
# Matching pattern
pmatch("gee", x) 
pmatch("Boo", x)

Output:

[1] "Geeks" "Books" "geek" 
[1] 3
[1] 2

Example 2:




# R Program to match pattern in a string
  
# Creating string vector
x <- c("Geeks", "Books", "geek")
x
  
# Matching pattern
pmatch(c("Gee", "Boo", "re"), x, nomatch = -1

Output:

[1] "Geeks" "Books" "geek" 
[1]  1  2 -1
Article Tags :