title: PHP Regex for matching a UNC path
body: <p>I'm after a bit of regex to be used in PHP to validate a UNC path passed through a form. It should be of the format:</p>
  
  <pre><code>\\server\something
  </code></pre>
  
  <p>... and allow for further sub-folders. It might be good to strip off a trailing slash for consistency although I can easily do this with substr if need be.</p>
  
  <p>I've read online that matching a single backslash in PHP requires 4 backslashes (when using a "C like string") and think I understand why that is (PHP escaping (e.g. 2 = 1, so 4 = 2), then regex engine escaping (the remaining 2 = 1). I've seen the following two quoted as <em>equivalent</em> suitable regex to match a single backslash:</p>
  
  <pre><code>$regex = "/\\\\/s";
  </code></pre>
  
  <p>or apparently this also:</p>
  
  <pre><code>$regex = "/[\\]/s";
  </code></pre>
  
  <p>However these produce different results, and that is slightly aside from my final aim to match a complete UNC path.</p>
  
  <p>To see if I could match two backslashes I used the following to test:</p>
  
  <pre><code>$path = "\\\\server";
  echo "the path is: $path &lt;br /&gt;"; // which is \\server
  $regex = "/\\\\\\\\\/s";
  if (preg_match($regex, $path)) 
  {
      echo "matched";
  }
  else
  {
      echo "not matched";
  }
  </code></pre>
  
  <p>The above however seems to match on two <em>or more</em> backslashes :( The pattern is 8 slashes, translating to 2, so why would an input of 3 backslashes (<code>$path = "\\\\\\server"</code>) match?</p>
  
  <p>I thought perhaps the following would work:</p>
  
  <pre><code>$regex = "/[\\][\\]/s";
  </code></pre>
  
  <p>and again, no :(</p>
  
  <p>Please help before I jump out a window lol :)</p>