douwen1549 2018-08-28 15:27 采纳率: 100%
浏览 222

正则表达式检查有效的目录路径

I'm playing around with regex, and I want to check valid folder path. This is the code.

return $folder->path('../../folder');

How to check if ../../ is valid? It can be ./../ or .././ or ...//./`. Can it be done with regex or something else?

  • 写回答

1条回答 默认 最新

  • dongqiang1894 2018-08-28 17:28
    关注

    Too long for a comment, so I'm posting this here

    The title of the answer is confusing. You tell about checking some valid directory. But reading the comments a more appropiate title would have been something like "Validate custom rules against a path-like string".

    Where your rules seem to be (according to the comments)

    1. It may start with any (or 0) number of ../
    2. Probably (need confirmation) you don't want to allow standalone . or .. wihin the rest of the directory (excluding prefix)
    3. The path (excluding prefix) may start as text or /text
    4. Probably (need confirmation) you don't want to allow 2 or more consecutive slashes

    If so, this may work for you: ^(\.\.\/(?:\.\.\/)*)?(?!.*?\/\/)(?!(?:.*\/)?\.+(?:\/|$)).+$

    See demo

    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路