stringr中的str_extract_all不捕获所有标点符号

时间:2017-07-13 02:49:21

标签: r regex string stringr

我有一个值,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中的错误还是我的代码有问题?

0 个答案:

没有答案