I want to select every lines in which we can find the expression "X01" or "X02" :
dataEx <- data.frame(code = c("X01-X043","X034","X024","X015-X036-X033","X012","X015-X042","X019","X036","X022-X043"),res = NA )
pat1 <- c("(^|-)X01($|-|.)","(^|-)X02($|-|.)")
dataEx$res[grep(paste(pat1,collapse="|"),dataEx$code)] <- "ok"
It works correctly and gives me the result :
code res
1 X01-X043 ok
2 X034 <NA>
3 X024 ok
4 X015-X036-X033 ok
5 X012 ok
6 X015-X042 ok
7 X019 ok
8 X036 <NA>
9 X022-X043 ok
But I would like to know which pattern is found :
code res
1 X01-X043 X01
2 X034 <NA>
3 X024 X024
4 X015-X036-X033 X015
5 X012 X012
6 X015-X042 X015
7 X019 X019
8 X036 <NA>
9 X022-X043 X022
I am very new to regular expression. Is there an easy way to do it ? (In reality, "pat1" is much longer, I am looking for 20 different patterns)