Home > Blockchain >  Set up a regexp - R
Set up a regexp - R

Time:08-19

I am struggling with regexp in R. I just want to check if a string contains ONLY alphanumeric or any of these symbols ?._-#$

I am trying with the following to find strings that contain any other symbol:

grepl("[^ 0-9a-zA-Z]|\\?|\\$|\\-|\\_|\\#]",xx)

I made a check with xx = "Ra?a" and it returns TRUE. Why is that so? What am I doing wrong?

CodePudding user response:

Just use grepl with a single character class:

x <- c("Hello", "Hello!")
grepl("^[A-Za-z0-9?._#$-] $", x)

[1]  TRUE FALSE
  • Related