Search code examples
rregexalphanumeric

How to recognise and extract alpha numeric characters


I want to extract alphanumeric characters from a partiular sentence in R. I have tried the following:

aa=grep("[:alnum:]","abc")

.This should return integer(0),but it returns 1,which should not be the case as "abc" is not an alphanumeric. What am I missing here? Essentially I am looking for a function,that only searches for characters that are combinations of both alphabets and numbers,example:"ABC-0112","PCS12SCH" Thanks in advance for your help.


Solution

  • [[:alnum:]] matches alphabets or digits. To match the string which contains the both then you should use,

    x <- c("ABC", "ABc12", "--A-1", "abc--", "89=A")
    grep("(.*[[:alpha:]].*[[:digit:]]|.*[[:digit:]].*[[:alpha:]])", x)
    # [1] 2 3 5
    

    or

    which(grepl("[[:alpha:]]", x) & grepl("[[:digit:]]", x))
    # [1] 2 3 5