📜  R编程中字符串中的模式匹配——agrep()函数

📅  最后修改于: 2022-05-13 01:55:05.058000             🧑  作者: Mango

R编程中字符串中的模式匹配——agrep()函数

R 语言中的agrep()函数用于在给定字符串的每个元素中搜索与模式的近似匹配。

示例 1:

Python3
# R program to illustrate
# agrep function
 
# Creating string vector
x <- c("GFG", "gfg", "Geeks", "GEEKS")
 
# Calling agrep() function
agrep("gfg", x)
agrep("Geeks", x)
agrep("gfg", x, ignore.case = TRUE)
agrep("Geeks", x, ignore.case = TRUE)


Python3
# R program to illustrate
# agrep function
 
# Creating string vector
x <- c("GFG", "gfg", "Geeks", "GEEKS")
 
# Calling agrep() function
agrep("gfg", x, ignore.case = TRUE, value = TRUE)
agrep("G", x, ignore.case = TRUE, value = TRUE)
agrep("Geeks", x, ignore.case = FALSE, value = FALSE)
agrep("GEEKS", x, ignore.case = FALSE, value = FALSE)


输出 :

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

示例 2:

Python3

# R program to illustrate
# agrep function
 
# Creating string vector
x <- c("GFG", "gfg", "Geeks", "GEEKS")
 
# Calling agrep() function
agrep("gfg", x, ignore.case = TRUE, value = TRUE)
agrep("G", x, ignore.case = TRUE, value = TRUE)
agrep("Geeks", x, ignore.case = FALSE, value = FALSE)
agrep("GEEKS", x, ignore.case = FALSE, value = FALSE)          

输出:

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