-1

I have this list of strings:

stringg <- c("csv.asef", "ac ed", "asdf$", "asdf", "dasf]", "sadf {sadf")

if I want to get all strings containing special characters like so:

grep("[:punct:]+", stringg, value = TRUE)
--------------------------------------------
Result:
[1] "csv.asef" "ac ed"

What I should get is:
[1] "csv.asef" "asdf$" "dasf]" "sadf {sadf"

if I use:

grep("[!\\"#$%&’()*+,-./:;<=>?@[]^_`{|}~.]+", stringg, value = TRUE)
-----------------------------------------------------------------
Result is ERROR

I want these special characters: € ! " # $ % & ’ ( ) * + , - . / : ; < = > ? @ [ ] ^ _ ` { | } ~. which [:punct:] doesn't have

I know if I want the strings not containing any of those characters then I would use:

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

but how do I do it with [:punct:]:

[^:punct:]?
[^:punct:]{0}?

and how could i combine ^[:punct:] | ^€ ?

many thanks

thelatemail
  • 91,185
  • 12
  • 128
  • 188
TheDev
  • 153
  • 5
  • Some of them are meta i.e. `$` signifies the end of the string – akrun Nov 03 '20 at 22:48
  • 2
    Try with `grep("[[:punct:]]+", stringg, value = TRUE)` – akrun Nov 03 '20 at 22:50
  • Yep, with one set of `[]` you're searching for `grep("[:cnptu]+", stringg, value = TRUE)` that is any character in the set of `:` or `c` or `n` .... – thelatemail Nov 03 '20 at 22:52
  • thanks: i used this grep("[([:punct:]|€)+]", stringg, value = TRUE) worked fine – TheDev Nov 03 '20 at 22:54
  • how can I use grep("[^([:punct:]|€)]", stringg, value = TRUE) this is false, i want the strings not containing those character grep("[([:punct:]|€){0}]", stringg, value = TRUE) also not working – TheDev Nov 03 '20 at 22:57
  • 1
    See https://stackoverflow.com/questions/42013292/posix-character-class-does-not-work-in-base-r-regex, the main problem you have is that you used the POSIX character class outside of a bracket expression. – Wiktor Stribiżew Nov 03 '20 at 23:03

1 Answers1

0

According to ?regex

Most metacharacters lose their special meaning inside a character class. To include a literal ], place it first in the list.

grep("[[:punct:]]+", stringg, value = TRUE)
#[1] "csv.asef"   "asdf$"      "dasf]"      "sadf {sadf"

If we want the opposite, use invert = TRUE

grep("[[:punct:]€]", stringg, value = TRUE, invert = TRUE)
#[1] "ac ed" "asdf" 
akrun
  • 874,273
  • 37
  • 540
  • 662