check capital words in text and extract it

百般思念 提交于 2019-12-30 09:58:33

问题


I want to extract all capital words from the text. Lets say my data is like-->

Text<-c('I am JAY','I AM NOT HAPPY','YOU ARE IRRITATING','so Funny','hEY)

So output should be like -->

> output

[1] "I JAY" "I AM NOT HAPPY" "YOU ARE IRRITATING" "" ""

Please help me for this.


回答1:


Another option is

library(stringr)
sapply(str_extract_all(Text, '\\b[A-Z]+\\b'), paste, collapse=' ')
# [1] "I JAY"              "I AM NOT HAPPY"     "YOU ARE IRRITATING"
#[4] ""                   ""    

Or

 gsub("[a-z][A-Za-z]+|[A-Za-z][a-z]+", '', Text)
 #[1] "I  JAY"             "I AM NOT HAPPY"     "YOU ARE IRRITATING"
 #[4] " "                  ""                  

data

 Text<-c('I am JAY','I AM NOT HAPPY','YOU ARE IRRITATING','so Funny','hEY')


来源:https://stackoverflow.com/questions/27223366/check-capital-words-in-text-and-extract-it

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!