title: What Regular Expression Will Return A Network Drive Name Only?
body: <p>My program reads in a string list which contains both file and folder names located on a network drive. </p>
  
  <p>Here are some examples of what my list can contain:</p>
  
  <pre><code>\\NETWORKDRIVE\text1.txt
  \\NETWORKDRIVE\\text2.txt
  \\NETWORKDRIVE\foldername\text3.txt
  \\NETWORKDRIVE\\
  \\NETWORKDRIVE\
  \\NETWORKDRIVE
  </code></pre>
  
  <p>I need a regular expression to return the network drive name only (including the leading "\" characters).</p>
  
  <p>I have tried various regular expressions and I have found one that almost works. Here is what I have currently:</p>
  
  <pre><code>^(\\+)(.*?)(\\+)
  </code></pre>
  
  <p>There are two problems with this regular expression.  </p>
  
  <p>The first problem is that it matches the network drive name <em>and</em> the trailing "\" characters. The second problem is that if the network drive name doesn't end with any "\" characters it only matches the leading "\" characters.</p>
  
  <p>Here are my results to demonstrate:</p>
  
  <pre><code>\\NETWORKDRIVE\text1.txt =&gt; \\NETWORKDRIVE\
  \\NETWORKDRIVE\\test2.txt =&gt; \\NETWORKDRIVE\\
  \\NETWORKDRIVE\foldername\text3.txt =&gt; \\NETWORKDRIVE\
  \\NETWORKDRIVE\\ =&gt; \\NETWORKDRIVE\\
  \\NETWORKDRIVE\ =&gt; \\NETWORKDRIVE\
  \\NETWORKDRIVE =&gt; \\
  </code></pre>
  
  <p>As you can see I am not getting the results that I want. I simply want the result to be:</p>
  
  <pre><code>\\NETWORKDRIVE
  </code></pre>
  
  <p>for all cases.</p>
  
  <p>Can anyone help me modify and/or change my regular expression to do what I need it to do?</p>
  
  <p>Thank you very much.</p>