Grep in R using OR and NOT
I have the following vector in R and I would like to find all the string having A's and B's but not the number 2 in it. vec1<-c("A_cont_1", "A_cont_12", "B_treat_8", "AB_cont_22", "cont_21_Aa") The following does not work: grep("A|B|!2", vec1) It gives me back all the strings:  1 2 3 4 5 The same is true for this example: grep("A|B|-2", vec1) What would be the correct syntax?
You can do this with a fairly simple regular expression: grep("^[^2]*[AB][^2]*$", vec1) In words, it means: ^ match the start of the string [^2]* match anything except "2", zero or more times [AB] match "A" or "B" [^2]* match anything except "2", zero or more times $ match the end of the string
I would use two grep calls: intersect(grep("A|B",vec1),grep("2",vec1,invert=TRUE)) # 1 3
OP, your attempt is pretty close, try this: grep('^(A|B|[^2])*$', vec1)
grep generally does not work very well for doing a positive and a negative search in one invocation. You might be able to make it work with a complex regular expression, but you might be better off just doing: grep '[AB]' somefile.txt | grep -v '2' The R equivalent of that would be: grep("2", grep("A|B", vec1, value = T), invert = T)
Jmter complicated regular expression solution? [duplicate]
Google analytics filter RegEXp assistance
RegEx design a pattern to extract id from url?
Regular Expressions | Delete words on multiple lines before a given word
Findstr - Return only a regex match
Converting VB function to Regex?
how to put text after a line beginning with http?
How to find a regex in re2 to search between strings?
How to combine two lists in one list? using notepad++ regex? [duplicate]
Scala - Explanation for regex statement
Python Regular Expression Multiple Groups
Using regexp in Matlab to find specific file from beginning and end components of filename
regular expression to check ends with a digit
Regex to capture string until another string is encountered
Google apps script, getBody() from Gmail, regex \n
How can I do a search across multilines in Delphi's regex