可以在grepl()中使用AND运算符吗? [英] Is it possible to use an AND operator in grepl()?

查看:290
本文介绍了可以在grepl()中使用AND运算符吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想搜索以55开头的任何内容,以及其中包含单词Roof(区分大小写,对于有好奇心的人)的任何内容.

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.

到目前为止,我一直没有成功,因为我似乎只能使用OR运算符:

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)

grepl("*^55&*Roof", dataset$longname)

(很明显,这些方法都不起作用-它们仅用于说明.)

(Clearly, neither of these work - they're for illustration only.)

我希望我的结果显示任何以55开头并且在任何地方带有单词Roof的东西.

I want my results to show anything that begins with 55 and which has the word Roof anywhere in it.

例如,我希望这两个结果都可以通过grepl()函数获取:

For example, I would expect both of these results to be picked up by the grepl() function:

55 - Roof Structure

55-Wall to Roof

是否有AND运算符可以与grepl()一起使用?

Is there an AND operator that can be used with grepl()?

推荐答案

要搜索同时包含"a"和"b"的字符串

To search for a string having both "a" and "b"

grepl("a", x) & grepl("b", x)

grepl("a.*b|b.*a", x)

如果我们知道两个部分的顺序,则可以省略最后一个grepl的两半之一.例如,对于问题中的查询,这足以找到以55开头并包含Roof的字符串

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)

这篇关于可以在grepl()中使用AND运算符吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆