2016-11-06 80 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''。花一點時間閱讀我鏈接的教程。他們非常值得。 –

相關問題