Jason  Zhang
Jason Zhang

Reputation: 29

How to grep a string that only has alphanumerics in r?

for example, I want only the first two entries:

sttt=c("flyingFish1", "toadie23","h!toyou!")
k=grep("[[:alnum:]]", sttt, value = TRUE )
k

but this gives me all three entries.

Thank you

Upvotes: 1

Views: 2629

Answers (2)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626845

It looks like you want to omit any character vectors having a non-alphanumeric char.

Use

sttt=c("flyingFish1", "toadie23","h!toyou!")
k=grep("[^[:alnum:]]", sttt, value=TRUE, invert=TRUE)
k
# => [1] "flyingFish1" "toadie23" 

See the online R demo.

The [^[:alnum:]] is a negated bracket expression that will match any char but an alnum (alphanumeric) char. Since you need to get the items that are not matched with the pattern, use invert=TRUE that will fetch the right items.

Upvotes: 0

Dan
Dan

Reputation: 10786

Each of those strings does match [[:alnum:]] - that is, they all have at least one alphanumeric character! If you'd like to match against the entire string, try:

sttt=c("flyingFish1", "toadie23","h!toyou!")
k=grep("^[[:alnum:]]+$", sttt, value = TRUE )
k

I've added three characters to your regular expression:

  • ^ matches the beginning of the string
  • $ matches the end of the string
  • + allows the [[:alnum:]] class to be repeated one or more times.

Your original regex matched strings that contain an alnum. This regex matches strings that are made up entirely of alnums.

Upvotes: 3

Related Questions