我有一个值,mystring
定义如下:
mystring <- "! \" # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~"
当我尝试使用string_extract_all
函数提取所有标点符号时,无法提取$
和+
这样的标点符号。我试图用反斜杠逃避它们,但我会得到一个错误。
str_extract_all(mystring, pattern = "[[:punct:]]")
# [[1]]
# [1] "!" "\"" "#" "%" "&" "'" "(" ")" "*" "," "-" "." "/" ":" ";" # "?" "@" "[" "]" "_" "{" "}"
它虽然在基础grepl
中有效:
grep(pattern = "[[:punct:]]", unlist(strsplit(mystring," ")), value = TRUE)
# [1] "!" "\"" "#" "$" "%" "&" "'" "(" ")" "*" "+" "," "-" "." "/" ":" ";" "<" "=" ">" "?" "@"
# [23] "[" "]" "^" "_" "`" "{" "|" "}" "~"
这是stringr
中的错误还是我的代码有问题?