str_extract_all in stringr does not capture all punctuations

Viewed 55

I have a value, mystring defined below:

mystring <- "! \" # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~"

When I tried to extract all punctuation using string_extract_all function, some punctuation like $ and + could not be extracted. I tried to escape them with a backslash but I would get an error instead.

str_extract_all(mystring, pattern = "[[:punct:]]")
# [[1]]
#  [1] "!"  "\"" "#"  "%"  "&"  "'"  "("  ")"  "*"  ","  "-"  "."  "/"  ":"  ";"  # "?"  "@"  "["  "]"  "_"  "{"  "}"

It works in base grepl though:

grep(pattern = "[[:punct:]]", unlist(strsplit(mystring," ")), value = TRUE)
# [1] "!"  "\"" "#"  "$"  "%"  "&"  "'"  "("  ")"  "*"  "+"  ","  "-"  "."  "/"  ":"  ";"  "<"  "="  ">"  "?"  "@" 
# [23] "["  "]"  "^"  "_"  "`"  "{"  "|"  "}"  "~" 

Is this a bug in stringr or is there something wrong with my code?

0 Answers
Related