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.
aleckxe, I believe this is the one-liner you are looking for.
As suggested by chromate in the comment below, you could easily pipe to sort for a sorted list if you wished.
Let me know if you'd like any tweaks.
Explanation:
After the pipe
|
which redirects the output of thepip
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 matchdjango-
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 stringadmin
.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 containsomeword
, just replaceadmin
withsomeword
.The output:
(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.
One approach is the above mentioned method that pipes the pip search results to 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.
Seems PyPI search method doesn't support regexp. 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. You can create you own script as alternative for grep(But it isn't a lot of sense). Fast example: