Rで「-」で結合されている単語のみを識別する方法は?[複製]

Dec 13 2020

Rに次のテキストがあると仮定しましょう。

x = "The effects in the medium-term of an appreciation of the exchange rate are still to be carefully assessed in our projections. However, we can observe in the short-term that the our pogramme of purchasing asset-backed securities had a positive impact on overall economic activity"

次のものだけを取得するにはどうすればよいですか。

# medium-term
# short-term
# asset-backed 

基本的には、「-」でリンクされている単語のみを抽出する必要があります。

誰かが私を助けることができますか?

ありがとう!

回答

1 KarthikS Dec 13 2020 at 17:45

これは機能しますか:

library(stringr)
str_extract_all(x, '\\b[a-z]+-[a-z]+\\b')[[1]]
[1] "medium-term"  "short-term"   "asset-backed"
1 RonakShah Dec 13 2020 at 17:47

ベースRでは、次を使用できます。

regmatches(x, gregexpr('\\w+-\\w+', x))[[1]]
#[1] "medium-term"  "short-term"   "asset-backed"