A place for my personal musings, about programming, design or whatever come across my mind

Regex hammer time

When you have a hammer, you see everything as nails.
A very true message from Dive into Python 3,

Regular expressions are extremely powerful, but they are not the correct solution for every problem. You should learn enough about them to know when they are appropriate, when they will solve your problems, and when they will cause more problems than they solve.

But it is easy to fall into the trap of using regex for everything.
I need to find the last string from a path, where

path = "d:/some/directory-with dash-and space"

So here is how I try to get the last string in a path, with regex

path.match(/\w*$/)

trying to capture any word characters at the end of the string with \w*$, but only captured the last word “space”, not good.

path.match(/(\s|\w)*$/)[0]

to match either whitespace (\s) or word characters (\w) at the end of the string, the result is “and space”, dashed is not captured, not good.

path.match(/[^\/]*$/)

[^\/] to take in everything except “/”, result is “directory-with dash-and space”, it works!

Only later I found out there is a really simple way to do this

File.basename(path)

will actually give you “directory-with dash-and space”. Plus, it will ignore ending slash if there is any.
Duh!

Leave A Comment