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

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

Categories

HOME
printing
actions-on-google
bootstrap-4
spring-roo
intl
silverlight
wso2-das
light-inject
bluej
higher-order-functions
cytoscape.js
editor
gas
exe4j
game-maker-language
co
wso2cep
scalability
iar
nested-loops
memorystream
editorconfig
flatbuffers
coqide
svn2git
debugview
whatsapi
magento2.0.2
toastr
localdb
skyscanner
pyparsing
google-shared-contacts
imageshack
groove
stackexchange.exceptional
nib
delete-row
transpose
ipod-touch
message-hub
jpype
subscription
xargs
distributed-lock
invalidate
appdynamics
rating-system
ewsjavaapi
dump
maven-versions-plugin
untagged
waf
db2400
masspay
sqlite-net
hard-drive-failure
flattr
statistics-bootstrap
ng-annotate
4d
sql-server-2016-express
cucumber-java
dnx
laravel-forge
text-alignment
appv
gulp-protractor
viennacl
treegrid
textmatching
movie
conditional-statements
gcovr
imake
gnat
android-menu
notifydatasetchanged
short
fig
pydatalog
pdf-scraping
sshj
location-client
mainwindow
capitalize
git-gui
src
cocosbuilder
51degrees
jscience
expressionvisitor
process.start
xmltextwriter
remote-control
polyglot
chickenfoot
iequalitycomparer
usenet

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