1

I need to validate a user text entry for a directory based on Windows, there is no directory picker of sort, it is just a text field.

I have written the following Regex using http://regex101.com/ which works adequately for my needs:

^[a-zA-Z]\:[\/,\\].{1,}

Which will match for example the following directories in the online tool:

C:/Users/Charlie/Dropbox
D:/Users/Bob/Quotes
F:/Quotes

The problem is, when using it in my application, preg_match does not match any of the above. It does not return true, and the preg_last_error() returns 0 which also indicates a false return value.

The exact code used is:

if(preg_match('/^[a-zA-Z]\:[\/,\\].{1,}/', $directory)) { }

Any help much appreciated.

Charlie
  • 197
  • 4
  • 15
  • whats in $directory? – Chris Oct 20 '15 at 08:39
  • @Chris Any of the directories shown above, none of them work. Have echoed them on the server side (directly before the `preg_match`) and they print exactly as expected. – Charlie Oct 20 '15 at 08:42
  • try to define an output array and print it.. preg_match("/^[a-zA-Z]\:[\/,\\].{1,}/", $directory, $output_array); – Chris Oct 20 '15 at 08:44

1 Answers1

1

You need to use quadruple backslash to match a literal backslash here:

if(preg_match('/^[a-zA-Z]\:[\/,\\\\].{1,}/', 'C:/Users/Charlie/Dropbox')) {
//                             ^^^^

See IDEONE demo

Otherwise, the backslash is just escaping the ] and that ruins the regex character class.

Note that instead of {1,} you can just use + quantifier that means 1 or more occurrences and that you do not have to escape the colon.

I also do not understand why you have a comma in the character class, the character is treated as a literal. I think you just want to match either \ or / with [\/,\\\]? Then, the whole regex would look like

'/^[a-zA-Z]:[\/\\\\].+/'

See another demo

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • 1
    That did the trick, thanks! Though it's worth noting that I actually had to use a quadruple backslash, the triple seems to escape the final quote somehow, leaving the rest of the file in quotation marks. I have now replaced the `more than one` with the `+` quantifier. Comma was intended as a separator between the two characters yes, realise now that was incorrect, have updated this now. I have tested it and it's all working perfectly, thanks again. – Charlie Oct 20 '15 at 09:04
  • 1
    I have found a [post explaining that both 3 and 4 backslashes can be used to match a literal ``\``](http://stackoverflow.com/a/15369828/3832970) but it really recommends using 4 backslashes to match a literal ``\``. I updated the answer accordingly. I think the 3 escapes work the same way as in Python - when an escape sequence cannot be interpreted as such, the backslash is treated as a literal. Perhaps, not in all cases - such as yours. – Wiktor Stribiżew Oct 20 '15 at 09:19