2016-11-06 62 views
0

我想在R中的stringr中使用str_view来查找以“y”开头的所有单词以及以“x”结尾的所有单词。我有一个由Corpora生成的单词列表,但每当我启动代码时,它都会返回一个空白视图。将str_view与R中的单词列表一起使用

Common_words<-corpora("words/common") 

#start with y 
start_with_y <- str_view(Common_words, "^[y]", match = TRUE) 
start_with_y 

#finish with x 
str_view(Common_words, "$[x]", match = TRUE) 

另外,我想发现只有3个字母长的话,但没有 想法而已。

+2

请提供[MCVE(最小完全可验证例)](http://stackoverflow.com/help/mcve) 。 –

+0

对不起,你认为最小的完整可验证的例子是什么意思? – Rfanatic

+0

该链接解释了它? :)'str_view(s,'^ y(。)* x $')' – Gopala

回答

0

我想说这不是编程与stringr但学习一些正则表达式。下面是一些网站,我发现有用的学习:

这里\\w或短手类单词字符(即[A-Za-z0-9_])是有用的量词(在这两种情况下为+{3})。 PS在这里我使用stringi因为stringr反正在后端使用。只是跳过中间人。

x <- c("I like yax because the rock to the max!", 
    "I yonx & yix to pick up stix.") 

library(stringi) 

stri_extract_all_regex(x, 'y\\w+x') 
stri_extract_all_regex(x, '\\b\\w{3}\\b') 

## > stri_extract_all_regex(x, 'y\\w+x') 
## [[1]] 
## [1] "yax" 
## 
## [[2]] 
## [1] "yonx" "yix" 


## > stri_extract_all_regex(x, '\\b\\w{3}\\b') 
## [[1]] 
## [1] "yax" "the" "the" "max" 
## 
## [[2]] 
## [1] "yix" 

EDIT好像这些可能是使用的太:

## Just y starting words 
stri_extract_all_regex(x, 'y\\w+\\b') 

## Just x ending words 
stri_extract_all_regex(x, 'y\\w+x') 

## Words with n or more characters 
stri_extract_all_regex(x, '\\b\\w{4,}\\b') 
+0

谢谢,这太棒了! – Rfanatic

+0

嗨,你知道吗,使用这种相同类型的代码,我可以指定我想要6个字母以上的所有单词,而不是简单地重复6,7,8等代码?谢谢! – Rfanatic

+0

当然可以使用开放式量词''\\ b \\ w {6,} \\ b''。花一点时间阅读我链接的教程。他们非常值得。 –

相关问题