r - Returning non-alphanumeric characters found by REGEX -


i have no problem finding , returning words containing non-alphanumeric characters, i'd return non-alphanumeric character found. example:

a <- c("hello?", "goodbye","hi!") grep("[^[:alnum:]]", a, value=true) 

returns:

[1] "hello?" "hi!" 

but i'd return is:

[1] "?" "!" 

any thoughts? thanks!

edit: love this...two user responses, 4 different ways done. i've learned lot. thank you!

we can use gsub remove alphanumeric characters matching pattern ([^[:punct:]]+ - meaning 1 or more non punctuation characters) , replace blanks (""). remove blanks either nzchar or setdiff.

setdiff(gsub("[^[:punct:]]+", "", a), "") #[1] "?" "!" 

or option str_extract stringr

library(stringr) as.vector(na.omit(str_extract(a, "[[:punct:]]+"))) #[1] "?" "!" 

Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -