pip 正则表达式搜索

时间:2023-04-17
本文介绍了pip 正则表达式搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我需要在 PyPI 上找到与特定正则表达式匹配的所有包:

I need to find all packages on PyPI that match a particular regular expression:

^django-.*?admin.*$

基本上,包名应该以 django- 开头,后面有 admin 字样.例如,以下包应该匹配:

Basically, the package name should start with django- and have admin word after. For example, the following packages should match:

django-redis-admin
django-admin-ckeditor 
django-admintools-bootstrap

我可以做pip search django-,但是有大量我不感兴趣的包.

I can do pip search django-, but there is a huge amount of packages that I'm not interested in.

pip 是否提供了一种通过正则表达式查找包的方法?或者,我应该将 django- 的结果通过管道传送到 grep 以过滤掉不相关的包吗?

Does pip provide a way to find packages by a regex? Or, should I just pipe the results of django- to grep to filter out irrelevant packages?

另外,pip search django-pip search admin 的交叉点"可能也会有所帮助.

Also, probably an "intersection" of pip search django- and pip search admin would help too.

推荐答案

alecxe,我相信这就是你要找的单线.

alecxe, I believe this is the one-liner you are looking for.

pip search django | grep -P "^django-(?=[-w]*?admin)[-w]+"

正如 chromate 在下面的评论中所建议的那样,如果您愿意,您可以轻松地通过管道对排序列表进行排序.

As suggested by chromate in the comment below, you could easily pipe to sort for a sorted list if you wished.

pip search django | grep -P "^django-(?=[-w]*?admin)[-w]+" | sort

如果您需要任何调整,请告诉我.

Let me know if you'd like any tweaks.

解释:

| 管道将 pip 命令的输出重定向到 grep 命令的 之后,我们输入 grepPerl 模式 -P.这是必要的,否则我们将无法使用前瞻.

After the pipe | 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.

我们用 ^ 将模式锚定在字符串的开头,并立即将 django- 匹配为文字.然后我们断言(前瞻)在这个位置我们将能够匹配任意数量的破折号或单词字符(包括数字和下划线),然后是文字字符串 admin.

We anchor the pattern at the beginning of the string with ^ and immediately match django- as a literal. 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.

做出此断言(这是一种验证形式)后,我们现在可以匹配尽可能多的破折号和单词字符,这应该将我们带到模块名称的末尾.

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.

如果您想更改它以匹配包含 someworddjango- 模式,只需将 admin 替换为 someword.

If you ever wanted to change this to match django- patterns that contain someword, just replace admin with someword.

输出:

    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.

(不胜枚举.)

顺便看看 pip 搜索文档,我没有看到没有管道的方法.

By the way, looking at the pip search documentation, I don't see a way of doing this without the pipe.

这篇关于pip 正则表达式搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!