I want to delete strings which contains only special characters. If there is at least one letter or number the string should be left as is.
test_cases <- c("a", "&", "&& ", "& &", "& ", "&a", "& a", "1", "& 1", "&1", "& a d", "a ")
exp_out <- c("a", "", "", "", "", "&a", "& a", "1", "& 1", "&1", "& a d", "a ")
I used a negative look ahead for that:
gsub("^[^a-zA-Z0-9]+(?! *[a-zA-Z0-9]+ *)", "", test_cases, perl = TRUE)
# [1] "a" "" "" "" "" "&a" "& a" "1" "& 1" "&1" "& a d" "a "
This regex seems to be rather verbose and while testing I had to adapt it several times, because I forgot some edge cases. Thus, I was wondering whether I can come up with a "simpler" regex, that is a regex which is shorter?