Suppose I have a path in a string called \'/home/user/directory/HelloWorld.txt\'. I would like to remove the HelloWorld.txt, and end up with \'/home/user/directory\'. What rege
split on "/", remove last element and join them back.
$path='/home/user/directory/HelloWorld.txt';
@s = split /\// ,$path;
pop(@s);
print join("/",@s);
Don't use a regex. Instead, use File::Basename, which can handle all the special cases.
use File::Basename;
dirname("/foo/bar/baz/quux.txt"); --> "/foo/bar/baz"