Parsing File Names Using Regular Expressions

The problem at hand

data/oxford-iiit-pet/images/american_bulldog_146.jpg
data/oxford-iiit-pet/images/german_shorthaired_137.jpg
data/oxford-iiit-pet/images/japanese_chin_139.jpg
data/oxford-iiit-pet/images/great_pyrenees_121.jpg
data/oxford-iiit-pet/images/Bombay_151.jpg

The human way of doing things

The computer way of doing things

import re
string = 'data/oxford-iiit-pet/images/american_bulldog_146.jpg'
pat = r'([^/]+)_\d+.jpg$'
pat = re.compile(pat)
print(pat.search(string).group(1))
>american_bulldog

Conclusion

--

--

--

Software Engineering, Cloud and AI.

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
James Tan

James Tan

Software Engineering, Cloud and AI.

More from Medium

How to Consolidate Multiple Files Into One With Python

Pyscript — Python in your browser!

AVL Tree Data structure and it’s implementation in Python.

10 IMPORTANT PYTHON LIBRARIES / PACKAGES FOR DEVOLOPERS  :