[英]Pip regular expression search
I need to find all packages on PyPI
that match a particular regular expression:我需要在
PyPI
上找到与特定正则表达式匹配的所有包:
^django-.*?admin.*$
Basically, the package name should start with django-
and have admin
word after.基本上,package 名称应该以
django-
开头,后面有admin
字样。 For example, the following packages should match:例如,以下包应匹配:
django-redis-admin
django-admin-ckeditor
django-admintools-bootstrap
I can do pip search django-
, but there is a huge amount of packages that I'm not interested in.我可以做
pip search django-
,但是有大量我不感兴趣的包。
Does pip
provide a way to find packages by a regex? pip
是否提供了一种通过正则表达式查找包的方法? Or, should I just pipe the results of django-
to grep
to filter out irrelevant packages?或者,我应该只是
grep
django-
掉不相关的包?
Also, probably an "intersection" of pip search django-
and pip search admin
would help too.此外,可能
pip search django-
和pip search admin
的“交叉点”也会有所帮助。
I believe this is the one-liner you are looking for:我相信这是您正在寻找的单线:
pip search django | grep -P "^django-(?=[-\w]*?admin)[-\w]+"
You can pipe the output to sort
for a sorted list.您可以通过 pipe 和 output 对
sort
列表进行排序。
pip search django | grep -P "^django-(?=[-\w]*?admin)[-\w]+" | sort
You could also use egrep
(See also Difference between egrep and grep for the differences between both).您也可以使用
egrep
(另请参阅egrep 和 grep之间的差异了解两者之间的差异)。
pip search django | egrep "^django-[^ ]*?admin.*$" | sort
Explanation :说明:
After the pipe |
pipe后
|
which redirects the output of the pip
command to <stdin>
for the grep command, we enter grep in Perl mode -P
. which redirects the output of the
pip
command to <stdin>
for the grep command, we enter grep in Perl mode -P
. This is necessary, otherwise we would not be allowed to use a lookahead.这是必要的,否则我们将不允许使用前瞻。
We anchor the pattern at the beginning of the string with ^
and immediately match django-
as a literal.我们用
^
将模式锚定在字符串的开头,并立即将django-
匹配为文字。 We then assert (lookahead) that at this position we would be able to match any number of dashes or word characters (which include digits and underscores), followed by the literal string admin
.然后我们断言(前瞻)在这个 position 中,我们将能够匹配任意数量的破折号或单词字符(包括数字和下划线),然后是文字字符串
admin
。
Having made this assertion (which is a form of validation), we now match as many dashes and word characters as we can, which should take us to the end of the module name.做出这个断言(这是一种验证形式)后,我们现在可以匹配尽可能多的破折号和单词字符,这应该将我们带到模块名称的末尾。
There are several ways of expressing this and for this simple pattern the variations are largely a matter of preference or mood.有几种表达方式,对于这种简单的模式,变化主要取决于偏好或情绪。
If you ever wanted to change this to match django-
patterns that contain someword
, just replace admin
with someword
.如果您想更改它以匹配包含
django-
的someword
,只需将admin
替换为someword
。
The output : output :
django-smoke-admin - django-smoke-admin tests that all admin pages for all registered models responds correctly (HTTP 200).
django-adminskin - UNKNOWN
django-admin-exporter - Simple admin actions to download/export selected items in CSV, JSON, XML, etc.
django-treeadmin-fork-alt-storage - Tree UI for mptt-managed models, extracted from FeinCMS. This is a fork with support for alternative storage engines
django-relatedadminwidget - Get edit and delete links in your django admin. A utility class to let your model admins inherit from.
django-admin-langswitch - Adds easy language switch in admin
django-authy-admin - A drop in replacement for django's default admin site that provides two-factor authentication via authy's REST API.
django-frontendadmin - A a set of templatetags to allow an easy and unobstrusive way to edit model-data in the frontend of your page.
django-admin-app-names-singleton - Django admin enhancer
django-mobileadmin - The Django admin interface for mobile devices.
(The list goes on.) (名单还在继续。)
By the way, looking at the pip search documentation , I don't see a way of doing this without the pipe.顺便说一句,查看pip 搜索文档,我看不到没有 pipe 的方法。
Seems PyPI search method doesn't support regexp.似乎 PyPI 搜索方法不支持正则表达式。 Pip use xmlrpc for method calls but I coudn't find any info for
search (or any other) method not in docs or even with methodSignature or methodHelp for PyPI XML-RPC server.
Pip 使用 xmlrpc 进行方法调用,但我在文档中找不到任何搜索(或任何其他)方法的信息,甚至找不到用于 PyPI XML-RPC 服务器的 methodSignature 或 methodHelp。 You can create you own script as alternative for grep(But it isn't a lot of sense).
您可以创建自己的脚本作为 grep 的替代方案(但这没什么意义)。 Fast example:
快速示例:
from xmlrpclib import ServerProxy
import re
URL = 'https://pypi.python.org/pypi'
TEST_RE = r"^django-.*?admin.*$"
TEST = 'django-'
def main():
pypi = ServerProxy(URL)
res = pypi.search({'name': TEST})
for r in res:
m = re.match(TEST_RE, r['name'])
if m:
print(m.group(0))
if __name__ == '__main__':
main()
One approach is the above mentioned method that pipes the pip search results to grep.一种方法是上述方法,将 pip 搜索结果通过管道传输到 grep。 I recommend to use this if you want to use regex search once or rarely.
如果您想使用一次或很少使用正则表达式搜索,我建议使用它。
However if you need this feature regularly you should check out yip which is a package I wrote to accomplish regex search alongside other useful additions that pip's search can't do like displaying extra info(size, upload time, homepage or license) or colorizing the output for readability.但是,如果您经常需要此功能,您应该查看yip ,这是我编写的 package,用于完成正则表达式搜索以及 pip 搜索无法执行的其他有用添加,例如显示额外信息(大小、上传时间、主页或许可证)或着色output 以提高可读性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.