Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8794271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:13:45+00:00 2026-06-13T23:13:45+00:00

I try to build RegExp to validate(preg_match) some path string for two following rules:

  • 0

I try to build RegExp to validate(preg_match) some path string for two following rules:

  1. path must consists only symbols from given range [a-zA-z0-9-_\///\.]
  2. path will not consist an up directory sequence “..”

this is a correct path example: /user/temp

and the bad one: /../user

UPD:
/user/temp.../foo will also be correct (thanks to Laurence Gonsalves)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T23:13:46+00:00Added an answer on June 13, 2026 at 11:13 pm

    Consider this:

    $right_path = '/user/temp';
    $wrong_path = '/../user';
    $almost_wrong_path = 'foo/abc../bar';
    $almost_right_path = 'foo/../bar';
    
    $pattern = '#^(?!.*[\\/]\.{2}[\\/])(?!\.{2}[\\/])[-\w.\\/]+$#';
    var_dump(preg_match($pattern, $right_path)); // 1
    var_dump(preg_match($pattern, $wrong_path)); // 0
    var_dump(preg_match($pattern, $almost_wrong_path)); // 1
    var_dump(preg_match($pattern, $almost_right_path)); // 0
    

    I’ve actually built this pattern in three steps:

    1) the first rule given said that only symbols allowed in the string are 0-9, a-zA-Z, _ (underscore), - (hyphen), . (dot) and both slashes (/ and \). First three positions can be expressed with a shortcut (\w), others require a character class:

    [-\w.\\/]
    

    Note two things here: 1) hyphen should be either the first or the last symbol in the character class (otherwise it’s treated as a metacharacter used to define a range); 2) both dot and forward slash are not escaped yet (backslash is escaped, though; it’s too powerful to be left alone, even within [...] subexpression).

    2) now we have to make sure that the pattern does indeed cover the whole string. We do it with so-called anchors – ^ for beginning of the string, $ for the end. And, not to forget that our string may consist of one or more allowed symbols (this expressed with + quantifier). So the pattern becomes this:

    ^[-\w.\\/]+$
    

    3) one last thing – we have to prevent using ../ and ..\ (preceded by / or \ – or not, if ..[/\\] sequence begins the string) as well.

    The easiest way of expressing this rule is using so-called ‘negative lookahead‘ test. It’s written within (?!…) subexpression, and (in this case) describes the following idea: ‘make sure that sequence of zero or more symbols is not followed by “slash-two dots-slash” sequence’:

    ^(?!.*[\\/]\.{2}[\\/])(?!\.{2}[\\/])[-\w.\\/]+$
    

    One last thing is actually placing the pattern into preg_match function: as we use / symbol within the regex, we can just choose another set of delimiters. In my example, I chose ‘#’:

    $pattern = '#^(?!.*[\\/]\.{2}[\\/])(?!\.{2}[\\/])[-\w.\\/]+$#';
    

    See? It’s real easy. ) You just have to start from small things and gradually develop them.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try build a php regex that validate this type of input string: {name:'something
I try to build a nice box with only 1 image and 1 div.
Sometimes when i try to build/compile a downloaded source, i get following warning: ld:
I try build wrap panel with vertical buttons. Every button consist from image and
I try to build a JSF 2.0 Web application which is using libraries including
I try to build a metro-style-app with Javascript and when I run the Windows
i try to build a app that is in landsscape mode all the time.
Im try to build my plugins that sit in a seperate directory on the
I try to build libtorrent(rasterbar- libtorrent). It cannot be build. the build log is
I try to build project in Eclipse on Linux Ubuntu. Eclipse show error message:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.