user3245256
user3245256

Reputation: 1948

regex in R: how to exclude strings that contain some specific numeric values (length > 1)?

I have a vector of strings in R:

mystrings <- c("S0701",   "S0702",   "S0703",   "S0704",   "S0705",   "S0706",   "S0707",   "S0708",   "S0709",   "S0710",   "S0711",   "S0712",  
       "S0713",   "S0714",   "S07_oth") 

I want to exclude the last 3 strings using regex. I tried:

grep("S07[^oth|13]*$", mystrings, value = TRUE)

But this eliminates all strings that contain 10, 11, 12, 13, and 14. These don't work either:

grep("S07[^oth | (13|14))]*$", total, value = TRUE)
grep("S07[^(oth | 13 | 14)]*$", total, value = TRUE) 
grep("S07[^(oth | (13) | (14))]*$", total, value = TRUE) 
grep("S07[^oth | [1-1][3-4])*$", mystrings, value = TRUE)

Thank you for your help!

Upvotes: 1

Views: 134

Answers (1)

GordonShumway
GordonShumway

Reputation: 2056

you almost had it:

grep("oth|1[34]", mystrings, value = TRUE, invert = TRUE)

"S0701" "S0702" "S0703" "S0704" "S0705" "S0706" "S0707" "S0708" "S0709" "S0710" "S0711"
"S0712"

Upvotes: 6

Related Questions