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

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?

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