普通的前pression为不允许的父目录的文件路径 [英] Regular expression for file path which doesn't allow parent directories

查看:120
本文介绍了普通的前pression为不允许的父目录的文件路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在找编写必须与一些preFIX开始一个文件路径正则表达式。在这种情况下,它应该与'/罐/家居/'开始。我也想确保它不包含'/ .. - 没有跳起来的父目录

I'm looking to write a regex for a file path that must start with some prefix. In this case it should start with '/tank/home/'. I also want to make sure that it contains no '/..' -- no jumping up to parent directories.

我花了一段时间,也没说有什么发现完全正确摆弄周围。我定居在使用两个正则表达式,其必须匹配所述第一和第二,必须不匹配

I spent a while fiddling around without coming up with anything quite right. I settled on using two regexes, the first which must match and the second which must not match:

'^/tank/home/'

'/\.\.(/.*)?$'

这是不是做什么,我认为它呢?是否有更简单的方法?

Does this do what I think it does? Is there an easier way?

这是一个bash脚本,因为它的价值。

This is in a bash script, for what it's worth.

推荐答案

您可以扩大DAV的正则表达式包含一个额外的结尾斜杠:

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/"

这篇关于普通的前pression为不允许的父目录的文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆