Remove all text between two brackets

后端 未结 5 1394
礼貌的吻别
礼貌的吻别 2020-11-27 21:23

Suppose I have some text like this,

text<-c(\"[McCain]: We need tax policies that respect the wage earners and job creators. [Obama]: It\'s harder to save         


        
5条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-27 21:29

    I think this technically answers what you've asked, but you probably want to add a \\: to the end of the regex for prettier text (removing the colon and space).

    library(stringr)
    str_replace_all(text, "\\[.+?\\]", "")
    
    #> [1] ": We need tax policies that respect the wage earners..."
    

    vs...

    str_replace_all(text, "\\[.+?\\]\\: ", "")
    #> [1] "We need tax policies that respect the wage earners..." 
    

    Created on 2018-08-16 by the reprex package (v0.2.0).

提交回复
热议问题