Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

r check if string contains special characters

Tags:

string

r

grepl

I am checking if a string contains any special characters. This is what I have, and its not working,

    if(grepl('^\\[:punct:]', val))

So if anybody can tell me what I am missing, that will be helpful.

Special characters

        ~ ` ! @# $ % ^ & * | : ; , ." |
like image 480
Jill Sellum Avatar asked Apr 29 '16 03:04

Jill Sellum


1 Answers

As @thelatemail pointed out in the comments you can use:

grepl('[^[:punct:]]', val)

which will result in TRUE or FALSE for each value in your vector. You can add sum() to the beginning of the statement to get the total number of these cases.

You can also use:

grepl('[^[:alnum:]]', val)

which will check for any value that is not a letter or a number.

like image 153
octothorpe_not_hashtag Avatar answered Sep 27 '22 22:09

octothorpe_not_hashtag