Regular expression for file path which doesn't allow parent directories

徘徊边缘 提交于 2019-12-06 06:53:18

You can expand Dav's regex to include an extra trailing slash:

^(?!.*/\.\./.*)/tank/home.*$

But... a better option might be to make sure that the result of the path is something that starts under /tank/home:

FILEPATH=$(readlink -f $YOURFILE)
[[ $FILEPATH =~ ^/tank/home/ ]] && echo "starts with /tank/home/"

You could use a negative lookahead to make sure that there aren't any /.. in the string:

^(?!.*/\.\..*)/tank/home.*$
'^/tank/home(?!.*/\.\.(/|$))/' 

matches /tank/home/foo..bar but not /tank/home/.. or /tank/home/foo/../bar

You could use negative lookbehind too:

/tank/home/([^/]|?(<!/..)/)+$

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!