R中返回正则表达式中匹配字数的语法是什么?
R包:stringr::words 应用以下正则表达式后,我想知道stringr::words文件中正好有三个字母长的单词数:R中返回正则表达式中匹配字数的语法是什么?,r,regex,string,stringr,R,Regex,String,Stringr,R包:stringr::words 应用以下正则表达式后,我想知道stringr::words文件中正好有三个字母长的单词数: x <- str_view(words, "^...$", match = TRUE) 代码返回8,这不能为空,因为很明显x大于8 与正则表达式(在本例中为x)匹配后,计算字数的正确语法是什么 另外,有人能解释一下为什么在上面的例子中,length(x)返回8吗 提前谢谢。我建议将grep与长度一起使用: 长度(grep(“^.{3}$”,单
x <- str_view(words, "^...$", match = TRUE)
代码返回8,这不能为空,因为很明显x大于8
与正则表达式(在本例中为x)匹配后,计算字数的正确语法是什么
另外,有人能解释一下为什么在上面的例子中,length(x)返回8吗
提前谢谢。我建议将
grep
与长度一起使用:
长度(grep(“^.{3}$”,单词))
# => [1] 110
使用grep
,您实际上会得到单词的子集
,length
将返回找到的匹配项的计数
可以用来查看正则表达式匹配的HTML呈现,它实际上不返回匹配列表。在grep
旁边,您可以使用stringr::str_subset
str_view
返回用于查看的HTML对象
x <- str_view(words, "^...$", match = TRUE)
class(x)
#[1] "str_view" "htmlwidget"
不要使用str\u视图
使用str\u子集
:
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
库(stringr)
x另一个选项是str\u count
:
library(stringr)
sum(str_count(x, "^...$"))
[1] 3
数据:
谢谢你,罗纳克。如果你不介意的话,我还有一个后续问题。在上面的例子中,名称(x)的组成部分有多重要?我的意思是,如果有的话,它们的意义是什么,你知道吗?另外,您能解释一下为什么下面的代码不返回所有三个字母的单词吗?str_子集(单词,c(“^y”,“x$”,“^…$”,“…”))我期待不同条件的匹配,但似乎其中一些条件被忽略了。谢谢。str_view
仅用于查看目的,因此这些名称和其中的信息对于任何类型的字符串操作都不是很重要。此外,不能在同一stru子集
命令中包含多个条件。如果您希望它们的输出不同,请分别传递它们stru子集(单词“^y”)
,stru子集(单词“^x”)
等。谢谢,非常有用。你好,谢谢你,克里斯。也很有帮助。
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
library(stringr)
sum(str_count(x, "^...$"))
[1] 3
x <- c("abc", "abcd", "ab", "abc", "abcsd", "edf")