Bernardo Wilberger
Bernardo Wilberger

Reputation: 85

Match last dir before filename in unix path

I need to find the regex to extract the last dir before the file in full path strings like these:

./Resources/views/product/edit.html.twig
./Resources/views/product/show.html.twig
./Resources/views/product/new.html.twig

So these strings always yield 'product'.

Upvotes: 1

Views: 63

Answers (1)

Jan
Jan

Reputation: 43169

You may use

([^/]+)/[^/]*$

See a demo on regex101.com.
Broken apart, this says:

([^/]+) # capture anything not a /, at least once
/       # a /
[^/]*   # 0+ not /
$       # end of the line

Depending on the actual language and/or delimiters used, you may need to escape the forward slashes to \/.

Upvotes: 1

Related Questions