Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pip regular expression search

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

^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

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

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?

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

like image 763
alecxe Avatar asked Apr 17 '14 16:04

alecxe


People also ask

What is regex search in Python?

A RegEx, or Regular Expression, is a sequence of characters that forms a search pattern. RegEx can be used to check if a string contains the specified search pattern.

How do you find a regular expression?

Regular expressions are a flexible and powerful notation for finding patterns of text. To use regular expressions, open either the Find pane or the Replace pane and select the Use check box. When you next click Find Next, the search string is evaluated as a regular expression.

How do I install a regular expression module in Python?

Type "cmd" in the search bar and hit Enter to open the command line. What is this? Type “ pip install regex ” (without quotes) in the command line and hit Enter again. This installs regex for your default Python installation.

Do I need to install regex in Python?

Python does not provide an inbuilt regex module. You need to install it using the pip command and then import it into your Python IDE. Then we stored some text in a variable named string.


1 Answers

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

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

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.

Explanation:

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.

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.

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

The 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.

like image 167
zx81 Avatar answered Oct 05 '22 08:10

zx81