问题
I want to search for anything that begins with 55 and anything that has the word Roof (case-sensitive, for those who are curious) in it.
So far I have been unsuccessful, as I can only seem to use the OR operator:
grepl("*^55|*Roof", dataset$longname)
Ultimately, I want to achieve something like this:
grepl("*^55&&*Roof", dataset$longname)
or
grepl("*^55&*Roof", dataset$longname)
(Clearly, neither of these work - they're for illustration only.)
I want my results to show anything that begins with 55 and which has the word Roof anywhere in it.
For example, I would expect both of these results to be picked up by the grepl() function:
55 - Roof Structure
and
55-Wall to Roof
Is there an AND operator that can be used with grepl()?
回答1:
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)
回答2:
I am reposting an answer by @Psidom as his was deleted but was scalable to n AND clauses. If @Psidom undeletes his response I will delete this one (I voted to undelete) but feel the answer is important for other searchers:
s <- c("55 - Roof Structure", "55-Wall to Roof", 'd 55 Roof')
grepl("(?=.*^55)^Roof", s, perl = TRUE)
## [1] TRUE TRUE FALSE
## 2 AND clauses
grepl("^(?=.*^55)(?=.*Roof)(?=.*Wall)", s, perl = TRUE)
## [1] FALSE TRUE FALSE
来源:https://stackoverflow.com/questions/45374799/is-it-possible-to-use-an-and-operator-in-grepl