【发布时间】:2015-07-16 16:57:22
【问题描述】:
我有一个包含文件名的文件(除其他外)。只有部分文件名位于文件的行首:
~/remove/me/myexec.pl /some/other/path/exec.pl
/yet/another/path/pipeit.pl | ~/remove/me/subdir/tome.pl
~/remove/me/deeply/nested/exec.pl
我想删除以~/remove/me 开头的任何文件的文件路径。我还希望删除 ~/remove/me 的所有子目录。
这是我想要的上述输出:
myexec.pl /some/other/path/exec.pl
/yet/another/path/pipeit.pl | tome.pl
exec.pl
不以~/remove/me开头的文件的路径必须单独保留。
我能得到的最接近的是使用这样的正则表达式:
s{~/remove/me/[^/]*?}{}gxms
但这不能正确处理子目录,给我以下输出:
myexec.pl /some/other/path/exec.pl
/yet/another/path/pipeit.pl | subdir/tome.pl
deeply/nested/exec.pl
谁能想出一个正则表达式来解决这个问题?
【问题讨论】:
-
文件名是用空格分隔还是可以用管道符号等其他标记分隔?例如,管道两侧没有空格:
/yet/another/path/pipeit.pl|tome.pl -
@HåkonHægland 文件名总是用空格分隔