How to get the previous word with regex?

cpu-word, regex

Solution

(\w+)\W+\w+$

matches the second-to-last word, captures it into backreference no. 1, then matches one or more non-word characters, then the last word and then EOL.

If you don't really want actual words but path elements to match (even if they contain non-word characters), then

([^\\:]+)\\[^\\]+$

might do a better job.

Edit: Added a `:` to the "second-to-last-word group" so the regex can handle relative paths, too.

Problem

I have a path like this one: C:\Development.TFS\Examn\R4-branch\Web\OrganisationManager\themes\blue\css And I need a regex to get the word "blue" out of there. I tried, but didn't find a solution yet. It's practically getting the word before the last word from that string. Please help, thank you

Original source