Reputation:
I have column names that are something like this:
colnames <- c("a81", "b82", "a181", "b182")
I want to extract only columns that have either 81 or 82 at the end and before those numbers (81 and 82) there should be a letter. I want to use regex in grepl
and the expected output here is
TRUE TRUE FALSE FALSE
What I tried so far is
grepl("[:alpha:][81-82]$", colnames)
Unfortunately, the code returns FALSE
for all entries. What am I doing wrong?
Upvotes: 0
Views: 648
Reputation: 98
I'd go with
grepl("[a-z](81|82)$", colnames)
[a-z] means any letter and the parentheses group any of the next 2 characters: i.e. either 81 OR (|) 82.
Upvotes: 2
Reputation: 887971
The [:alpha:]
needs to be changed to [[:alpha:]]
and instead of [81-82]
, it can be 8
followed by [1-2]
grepl("^[[:alpha:]]8[1-2]$", colnames)
#[1] TRUE TRUE FALSE FALSE
Or we can specify
grepl("^[A-Za-z]8[1-2]$", colnames)
Upvotes: 1