Str_extract_all in stringr does not capture all punctuation

I have a value mystring

defined below:

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

      

When I tried to extract all punctuation with a function string_extract_all

, some punctuation marks like $

and +

could not be extracted. I tried to escape them with a backslash, but 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 an error in stringr

, or that something is wrong with my code?

+3


source to share





All Articles