使用 grepl 查找 R 字符向量中任何字符串的匹配项

Jinku Hu 2023年1月30日 2021年7月14日
  1. 在 R 中使用 grepgrepl 函数搜索模式匹配
  2. 使用 grepl 匹配 R 字符向量中的任何字符串
使用 grepl 查找 R 字符向量中任何字符串的匹配项

本文将讨论使用 grepl 在 R 字符向量中查找任何字符串的匹配项的几种方法。

在 R 中使用 grepgrepl 函数搜索模式匹配

grep 用于字符向量中的模式匹配。它将模式参数作为正则表达式,相应地由函数匹配。grep 默认返回匹配元素的索引向量,但如果用户将 TRUE 分配给 value 参数,它也可以返回匹配元素的字符向量。另一方面,grepl 返回一个布尔值向量,指示相应的元素是否与模式匹配。下面的例子演示了字母匹配任何字母,我们得到了符合预期的所有元素匹配结果。

grep("[a-z]", letters)
grep("[a-z]", letters, value = TRUE)
grepl("[a-z]", letters)
> grep("[a-z]", letters)
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

> grep("[a-z]", letters, value = TRUE)
 [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v"
[23] "w" "x" "y" "z"

> grepl("[a-z]", letters)
 [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[18] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE

使用 grepl 匹配 R 字符向量中的任何字符串

grepl 函数可以匹配由相应模式提供的字符串的任何逻辑排列。请注意,默认情况下 grepl 不匹配不同的大小写字母。以下代码片段显示了与找到 The 的每个字符串匹配的第一个函数。另一方面,对 grepl 的下一次调用将匹配包含 Thethe 的单词。

words <- c("The", "licenses", "for", "most", "software", "are",
         "to", "share", "and", "change", "it.",
         "", "By", "contrast,", "the", "GNU", "General", "Public", "License",
         "free", "for", "all", "its", "users")

i <- grepl("The", words)
i
i <- grepl("The|the", words)
i
[1]  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[15] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE

[1]  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[15]  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE

或者,我们可以利用 if...else 函数为字符向量中每个匹配或不匹配的元素打印字符串值,如以下代码示例所示。请注意,第二个调用使用与包含 e-h 范围内字符的任何字符串进行匹配。

words <- c("The", "Them", "for", "most", "software", "are",
         "to", "share", "and", "change", "it.",
         "", "By", "contrast,", "the", "GNU", "General", "Public", "License",
         "free", "for", "all", "its", "users")

i <- ifelse(grepl("The|the", words), "Tr", "Fa")
i <- ifelse(grepl("[e-h]", words), "Tr", "Fa")
i
[1] "Tr" "Tr" "Fa" "Fa" "Fa" "Fa" "Fa" "Fa" "Fa" "Fa" "Fa" "Fa" "Fa" "Fa" "Tr" "Fa" "Fa"
[18] "Fa" "Fa" "Fa" "Fa" "Fa" "Fa" "Fa"

[1] "Tr" "Tr" "Tr" "Fa" "Tr" "Tr" "Fa" "Tr" "Fa" "Tr" "Fa" "Fa" "Fa" "Fa" "Tr" "Fa" "Tr"
[18] "Fa" "Tr" "Tr" "Tr" "Fa" "Fa" "Tr"
Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn

相关文章 - R Regex