r - R 中用于返回正则表达式中匹配的单词数的语法是什么?

标签 r regex string stringr

R 包:stringr::words

在应用以下正则表达式后,我想知道 stringr::words 文件中恰好三个字母长的单词数:

x <- str_view(words, "^...$", match = TRUE)

虽然代码能够提取恰好三个字母长的单词,但它并没有告诉我有多少单词。所以,我认为长度函数适合查找数字。

length(x)

代码返回 8,这不可能,因为很明显 x 大于 8。

在与正则表达式(在本例中为 x)匹配后计算单词数的正确语法是什么?

此外,谁能向我解释为什么 length(x) 在上面的例子中返回 8?

提前谢谢你。

最佳答案

str_view 返回一个用于查看的 HTML 对象。

x <- str_view(words, "^...$", match = TRUE)
class(x)
#[1] "str_view"   "htmlwidget"

您看到的 8 个组件是

names(x)
#[1] "x"             "width"         "height"        "sizingPolicy"  "dependencies" 
#[6] "elementId"     "preRenderHook" "jsHooks"    

代替 str_view 使用 str_subset :

library(stringr)

x <- str_subset(words, "^...$")
x

#  [1] "act" "add" "age" "ago" "air" "all" "and" "any" "arm" "art" "ask" "bad" "bag"
# [14] "bar" "bed" "bet" "big" "bit" "box" "boy" "bus" "but" "buy" "can" "car" "cat"
# [27] "cup" "cut" "dad" "day" "die" "dog" "dry" "due" "eat" "egg" "end" "eye" "far"
# [40] "few" "fit" "fly" "for" "fun" "gas" "get" "god" "guy" "hit" "hot" "how" "job"
# [53] "key" "kid" "lad" "law" "lay" "leg" "let" "lie" "lot" "low" "man" "may" "mrs"
# [66] "new" "non" "not" "now" "odd" "off" "old" "one" "out" "own" "pay" "per" "put"
# [79] "red" "rid" "run" "say" "see" "set" "sex" "she" "sir" "sit" "six" "son" "sun"
# [92] "tax" "tea" "ten" "the" "tie" "too" "top" "try" "two" "use" "war" "way" "wee"
#[105] "who" "why" "win" "yes" "yet" "you"


length(x)
#[1] 110

关于r - R 中用于返回正则表达式中匹配的单词数的语法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67458264/

相关文章:

php - php中mysqli字符串中的多词匹配

c - 返回文本中任意字符的位置

c++ - 检查字符串中每个字符串有多少个(简化)

R 用 NA 填充插值矩阵

r - 如何使用文本变量中的关键字为逻辑回归创建二元变量

regex - 在分隔符上从字符串末尾拆分固定次数

mysql - 如何在 SQL 友好的正则表达式中匹配标签外但不在标签内的 URL

r - 将数据帧的行作为参数传递给函数,同时保持其他参数不变

c# - 正则表达式 - 最长的可能值

python - 在文件中查找和替换字符串(MAC 地址)不起作用