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)
Match a Regular Expression by simple 2 cases:
Regex ignore Find and Replace in Notepad++
Perl: How to match a string that is not in a given string [duplicate]
glob2rx in R to get all cells whose last decimal is 5?
Finding file names without a specified character
Perl: quick replacing of occurrences of multiple words in an array
Extract resolution from string
How do I create a Scala Regex that is compiled using Java Pattern.COMMENTS?
Is there a way to search terms in order with RegexpQuery in lucene?
Regex to allow any charcter EXCEPT backslash
Regex: Match a condition, then find the first occurrence of another condition that precedes it on the same line. Possible?
Regex for URL routing - match alphanumeric and dashes except words in this list
R: gsub and capture
Trouble with regex in Perl
Duplicate symbols in regular expressions
NSRegularExpression to remove the rest codes