regex


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] 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] 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)

Related Links

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

Categories

HOME
redirect
bootstrap-4
google-search
android-sqlite
compiler-errors
watson
react-navigation
styling
quickblox
chronicle
jqgrid
application-cache
sony
sweetalert
sony-camera-api
splunk
akka.net
gcov
wso2-das
asciidoc
session-cookies
ellipse
plc
pywin32
hp-alm
multiprocessing
windows-applications
atg
montecarlo
cadvisor
mathdotnet
pyparsing
background-process
gradient-descent
delete-row
runge-kutta
spring-saml
otroslogviewer
rating
php-5.5
using
sharpziplib
masm32
paypal-webhooks
typesetting
dwr
ietf-netconf
jawr
plotrix
tachyon
mysql-notifier
surroundscm
ultratree
xendesktop
balsamiq
singly-linked-list
pager
docklight
pakyow
subroutine
tag-helpers
armv7
hjson
session-replication
phone-number
core-data-migration
stringr
mkpolyline
android-design-library
gcovr
kue
citymaps
team-explorer-everywhere
dataformat
disk-io
declspec
windows-phone-7.1
jxta
junit-runner
mongo-shell
colormatrix
polarssl
sugar.js
qdebug
loadlibrary
route-me
src
html5-apps
tempo
datadesign
vote
makumba
51degrees
adaboost
expression-encoder-sdk
hunchentoot
myisam
program-transformation
scramble
anemic-domain-model
user-testing

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App