使用grep for“和”代替“或” [英] Using grep for "and" instead of "or"
本文介绍了使用grep for“和”代替“或”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个网址列表:
网址< - c(xx.zip,zx.zip ,zz.rar)
我可以找到所有这样的zip文件:
zlst < - grep(zip,url)
zls< - url [zlst]
zls
[1]xx.zipzx.zip
如果我想找到所有的文件,包括z和zip,我该怎么做。如果是z或zip,我可以使用|,但它不适用于&。
zlst < - grep(z& zip,url)
zls< - url [zlst]
zls
字符(0)
$ c
想要的输出:
[1 ]zx.zip
解决方案外观顺序。只需两个字符串,这不是什么大问题:
grep(z。* zip | zip。* z,网址,value = TRUE)
#[1]zx.zip
在单个正则表达式模式afaik中指定一个逻辑与运算符,如OR运算符 |
。
I have a list of urls:
urls <- c("xx.zip", "zx.zip", "zz.rar")
I can find all the zip files like this:
zlst <- grep("zip", urls)
zls <- urls[zlst]
zls
[1] "xx.zip" "zx.zip"
If i want to find all the files including "z" AND "zip", how do I do that. If it was "z" OR "zip" i could use |, but it doesn't work with &.
zlst <- grep("z&zip", urls)
zls <- urls[zlst]
zls
character(0)
Wanted output:
[1] "zx.zip"
解决方案 You can specify the possible order of appearance. With just two strings, that's not a big deal:
grep("z.*zip|zip.*z", urls, value = TRUE)
# [1] "zx.zip"
There's no direct way of specifying a logical AND operator like the OR operator |
in a single regex pattern afaik.
这篇关于使用grep for“和”代替“或”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文