R grep: is there an AND operator?
Thanks to this answer, this regex seems to work. You want to use grepl()
which returns a logical to index into your data object. I won't claim to fully understand the inner workings of the regex, but regardless:
x <- c("imageUploaded,people.jpg,more,comma,separated,stuff", "imageUploaded", "people.jpg")
grepl("(?=.*imageUploaded)(?=.*people\\.jpg)", x, perl = TRUE)
#-----
[1] TRUE FALSE FALSE
R grep with 'AND' logic
You could try
grep('hive-jdbc.*standalone', jars) # 'hive-jdbc' followed by 'standalone'
or
grepl('hive-jdbc', jars) & grepl('standalone', jars) # 'hive-jdbc' AND 'standalone'
Grep in R using AND
Lookahead is not needed to do this, you can use the alternation operator in context.
grep('five.*six|six.*five', object)
If you know that "five" will precede "six" on the line, you don't even need the alternatives:
grep('five.*six', object)
Is it possible to use an AND operator in grepl()?
To search for a string having both "a" and "b"
grepl("a", x) & grepl("b", x)
or
grepl("a.*b|b.*a", x)
If we know the order of the two parts then we can omit one of the two halves of the last grepl
. For example, in the case of the query in the question this would be sufficient to find a string that starts with 55 and contains Roof
grepl("^55.*Roof", x)
Another possibility is
grep("a.*b", paste(x, x))
If the terms searched for have space then use a different sep=
in paste
.
Boolean operators on grep in a dataframe
Just use the or
in your regular expression.
people[!grepl("Yamazaki|Xavier", people$Name), ]
Name PhoneX
5 Edward Zachary 7573
6 Fiona Zachary 9457
OR / AND operator in grepl o str_detect
We may use ^(?!.*Wag).*RX4
for that, where (?!.*Wag)
is a negative lookahead throughout all the string. For instance,
grep('^(?!.*Wag).*RX4', c("Wag RX4 bag", "bag RX4 Wag", "bag RX4 bag"), perl = TRUE)
# [1] 3
So, in your case that would be
dplyr::filter(mtcars, grepl('^(?!.*Wag).*RX4', type, perl = TRUE))
# mpg cyl disp hp drat wt qsec vs am gear carb type
# 1 21 6 160 110 3.9 2.62 16.46 0 1 4 4 Mazda RX4
I'd agree that this pattern isn't really obvious, so you may want to do it in two steps, allowing to use usual AND and NOT:
dplyr::filter(mtcars, grepl('RX4', type) & !grepl('Wag', type))
# mpg cyl disp hp drat wt qsec vs am gear carb type
# 1 21 6 160 110 3.9 2.62 16.46 0 1 4 4 Mazda RX4
Related Topics
Split One Row into Multiple Rows
How to Hold Figure Position with Figure Caption in PDF Output of Knitr
How to Use a List as a Hash in R? If So, Why Is It So Slow
How to Pass Dynamic Column Names in Dplyr into Custom Function
Rcpp Function Check If Missing Value
Perform Multiple Paired T-Tests Based on Groups/Categories
Efficiently Computing a Linear Combination of Data.Table Columns
What Is Difference Between Dataframe and List in R
Test If an Argument of a Function Is Set or Not in R
Data.Table Join Then Add Columns to Existing Data.Frame Without Re-Copy
Change Values in Multiple Columns of a Dataframe Using a Lookup Table
How to Combine 2 Plots (Ggplot) into One Plot
Using Parallel's Parlapply: Unable to Access Variables Within Parallel Code
R Ggplot2: Labelling a Horizontal Line on the Y Axis with a Numeric Value
Dplyr - Group by and Select Top X %
Any Suggestions for How to Plot Mixem Type Data Using Ggplot2