Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Regex version number out of package names

I have a folder with a bunch of packages, each with their version in their name. Im trying to use regex to create a list with those versions to be used later.

Some examples:

Input:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

libpci-dev_3.5.2-1_amd64.deb
libpcre3-dev_8.39-12_amd64.deb
libaprutil1-ldap_1.6.1-4_amd64.deb
nftables_0.9.6-1~bpo10+1_amd64.deb
libkdb5-9_1.17-3+deb10u3_amd64.deb
libc-dev-bin_2.28-10+deb10u1_amd64.deb
libboost-locale1.67.0_1.67.0-13+deb10u1_amd64.deb

I want to delete the front of the package name as well all the amd64 and .deb as well as the _ or - surrounding the versions and then just leave the version of the package in the list

Expected output:

3.5.2-1
8.39-12
1.6.1-4
0.9.6-1~bpo10+1
9_1.17-3+deb10u3
2.28-10+deb10u1
1.67.0_1.67.0-13+deb10u1

I managed to select only the numbers, but im having issues with the last examples, where there is also letters involved

>Solution :

Versions are started after _, so 9_1.17-3+deb10u3 should be 1.17-3+deb10u3, and you can extract those using the following regex.

_(.*)_amd64\.deb

Python Example

import re

packages = """libpci-dev_3.5.2-1_amd64.deb
libpcre3-dev_8.39-12_amd64.deb
libaprutil1-ldap_1.6.1-4_amd64.deb
nftables_0.9.6-1~bpo10+1_amd64.deb
libkdb5-9_1.17-3+deb10u3_amd64.deb
libc-dev-bin_2.28-10+deb10u1_amd64.deb
libboost-locale1.67.0_1.67.0-13+deb10u1_amd64.deb"""

versions = re.findall(r"_(.*)_amd64\.deb", packages)
print(*versions, sep="\n")

OUTPUT

3.5.2-1
8.39-12
1.6.1-4
0.9.6-1~bpo10+1
1.17-3+deb10u3
2.28-10+deb10u1
1.67.0-13+deb10u1
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading