Regex R从字符串中提取项目
我试图提取这个给定字符串中包含两个相邻元音的所有单词Regex R从字符串中提取项目,regex,r,Regex,R,我试图提取这个给定字符串中包含两个相邻元音的所有单词 x <- "The team sat next to each other all year and still failed." x 试试这个。看演示 您可以将\w*放置在字符类之前和之后,以匹配“零个或多个”单词字符 x <- "The team sat next to each other all year and still failed." regmatches(x, gregexpr('\\w*[aeiou]{2}\
x <- "The team sat next to each other all year and still failed."
x
试试这个。看演示
您可以将\w*
放置在字符类之前和之后,以匹配“零个或多个”单词字符
x <- "The team sat next to each other all year and still failed."
regmatches(x, gregexpr('\\w*[aeiou]{2}\\w*', x))[[1]]
# [1] "team" "each" "year" "failed"
xwords[grepl([aeiou]{2}),words]
与stringr相同
library(stringr)
xx <- str_split(x, " ")[[1]]
xx[str_detect(xx, "[aeiou]{2}")]
## [1] "team" "each" "year" "failed."
您可以使用\\W
拆分以删除最后的标点符号。谢谢;我投票赞成hwnd的提议。我很欣赏使用regmatches(,gregexpr(.)
的答案。
words <-unlist(strsplit(x, " "))
words[grepl("[aeiou]{2}", words)]
#[1] "team" "each" "year" "failed."
> words <-unlist(strsplit(x, "[[:punct:] ]"))
> words[grepl("[aeiou]{2}", words)]
library(stringr)
xx <- str_split(x, " ")[[1]]
xx[str_detect(xx, "[aeiou]{2}")]
## [1] "team" "each" "year" "failed."
str_extract_all(x, "\\w*[aeiou]{2}\\w*")[[1]]
## [1] "team" "each" "year" "failed"