केवल उन शब्दों की पहचान कैसे करें जो 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

बेस आर में, आप उपयोग कर सकते हैं:

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