Remove text inside brackets, parens, and/or braces

前端 未结 4 511
情话喂你
情话喂你 2020-12-16 22:22

I am in need of a function that extracts any type of bracket ie (), [], {} and the information in between. I created it and get it to do what I want but I get an annoying w

4条回答
  •  囚心锁ツ
    2020-12-16 23:26

    Give this a shot. I prefer the stringr package! :)

    bracketXtract <- function(string, bracket = "all", include.bracket = TRUE){
      # Load stringr package
      require(stringr)
    
      # Regular expressions for your brackets
      rgx = list(square = "\\[\\w*\\]", curly  = "\\{\\w*\\}", round  = "\\(\\w*\\)")
      rgx['all'] = sprintf('(%s)|(%s)|(%s)', rgx$square, rgx$curly, rgx$round)
    
      # Ensure you have the correct bracket name
      stopifnot(bracket %in% names(rgx))
    
      # Find your matches
      matches = str_extract_all(string, pattern = rgx[[bracket]])[[1]]
    
      # Remove brackets from results if needed
      if(!include.bracket) 
        matches = sapply(matches, function(m) substr(m, 2, nchar(m)-1))
    
      unname(matches)
    }
    
    
    
    j <- "What kind of cheese isn't your cheese? {wonder} Nacho cheese! [groan] (Laugh)"  
    bracketXtract(j)
    # [1] "{wonder}" "[groan]"  "(Laugh)" 
    bracketXtract(j, bracket = "square")
    # [1] "[groan]"
    bracketXtract(j, include.bracket = F)
    # [1] "wonder" "groan"  "Laugh" 
    

提交回复
热议问题