pattern for matching a filepath
Can you please tell me the grep pattern for matching the following filepath:
开发者_如何学编程../any_directoryname/filename.txt
I only know the filename. The any_directoryname
keeps on changing.
Thanks in advance,
Regards
John
Try this:
\.\./[^/]+/filename.txt
This assumes only one directory. If it can be more than that, try
\.\./[^\r\n]+/filename.txt
If you know the filename and want to extract the path from the string, use
str_replace('/filename.txt', '', $full_path);
A regex is not reliable if both file and path vary (you can only find out if something is a file or directory using filesystem commands, name checking is not going to tell you that), and if one of the two doesn't vary, you don't need a regex at all.
try this regex
^\.\.\/[\w]+\/[\w\.]+$
How about...
(A Perl answer...)
For a given path/filename in the form of:
$fullPath = /dir/dir/dir/filename
($path, $file) = $fullPath =~ m/(^\/.*\/)(\S+$)/;
That puts
$path = /dir/dir/dir/...
$file = filename
精彩评论