python - pip正则表达式搜索

标签 python regex pip package

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

^django-.*?admin.*$

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

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

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

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

此外,pip search django-pip search admin 的“交集”可能也会有所帮助。

最佳答案

我相信这就是您正在寻找的单行代码:

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

您可以将输出通过管道传输到 sort对于排序列表。

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

你也可以使用 egrep (另请参阅 Difference between egrep and grep 以了解两者之间的差异)。

pip search django | egrep "^django-[^ ]*?admin.*$" | sort

解释:

后管|重定向 pip 的输出命令 <stdin>对于 grep 命令,我们在 Perl 模式下输入 grep -P .这是必要的,否则我们将不允许使用前瞻。

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

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

有多种表达方式,对于这种简单的模式,变化主要取决于偏好或心情。

如果您想更改它以匹配 django-包含 someword 的模式, 只需替换 adminsomeword .

输出:

    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 search documentation ,如果没有管道,我看不到这样做的方法。

关于python - pip正则表达式搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23138238/

相关文章:

java - 正则表达式清除 csv 中令人困惑的字符

regex - NGINX Ingress Controller 用于分支部署的多种服务

python - 配置 pip 以从 pypi.python.org 获取丢失的包

python - 命令 'llvm-gcc-4.2' 失败,退出状态 1

python - 导入 skimage 不是有效的 Win32 应用程序 python3

python - 在 python 中创建出版质量表

python - 当一个对象可以等于不同类型的对象时,如何定义 __hash__ ?

用于文件更改的 Python 脚本监视器

python - Django:动态添加应用程序作为插件,自动构建 url 和其他设置

Mysql:将带有数字和字母的一列拆分为两列