使用 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