Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex R从字符串中提取项目_Regex_R - Fatal编程技术网

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"
x
words[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"