How to escape closed bracket “]” in regex in R

前端 未结 2 1572
醉梦人生
醉梦人生 2021-01-17 13:21

I\'m trying to use gsub in R to replace a bunch of weird characters in some strings I\'m processing. Everything works, except whenever I throw in \"]\" it make

相关标签:
2条回答
  • 2021-01-17 14:00

    You can switch the order of the character class without escaping.

    name <- 'R U Still Down? [Remember Me][*[[]*'
    gsub('[][?*]', '', name)
    # [1] "R U Still Down Remember Me"
    

    If you want to remove all punctuation characters, use the POSIX class [:punct:]

    gsub('[[:punct:]]', '', name)
    

    This class in the ASCII range matches all non-controls, non-alphanumeric, non-space characters.

    ascii <- rawToChar(as.raw(0:127), multiple=T)
    paste(ascii[grepl('[[:punct:]]', ascii)], collapse="")
    # [1] "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"
    
    0 讨论(0)
  • 2021-01-17 14:19

    Just enable perl=TRUE parameter.

    > gsub("[?\\]\\[*]", "", name, perl=T)
    [1] "R U Still Down Remember Me"
    

    And escape only the needed characters.

    > gsub("[()*$+?'\\[\\]]", "", name, perl=T)
    [1] "R U Still Down Remember Me"
    
    0 讨论(0)
提交回复
热议问题