title: How to use Path.GetInvalidPathChars to find a file name inside a larger string?
body: <p>My goal is to find a file name ("MyFile.txt") inside a larger string. I.e.:</p>
  
  <blockquote>
    <p><em>Some text before</em> <strong>MyFile.txt</strong> <em>some other text after</em></p>
  </blockquote>
  
  <p>Currently I'm successfully using a Regular Expression with a character class of something like the following (simplified):</p>
  
  
  
  <pre><code>[\w\.\-]
  </code></pre>
  
  <p>This works fine, until the file contains other characters that are outside the <code>\w</code> group, e.g. an em dash: "My—File.txt".</p>
  
  <p><strong>My approach:</strong></p>
  
  <p>The method <a href="https://msdn.microsoft.com/en-us/library/system.io.path.getinvalidpathchars" rel="nofollow noreferrer"><code>Path.GetInvalidPathChars</code></a> returns an array of <em>invalid</em> characters. I've tried to use this method. Unfortunately, I found no way of "converting" this to be useful inside a Regular Expression.</p>
  
  <p>I'm aware of</p>
  
  <ul>
  <li>The SO posting "<a href="https://stackoverflow.com/questions/146134/how-to-remove-illegal-characters-from-path-and-filenames">How to remove illegal characters from path and filenames?</a>"</li>
  <li>The concept of "<a href="https://msdn.microsoft.com/en-us/library/20bw873z.aspx?f=255&amp;MSPPError=-2147217396#CharacterClassSubtraction" rel="nofollow noreferrer">Character class subtraction</a>"</li>
  </ul>
  
  <p>Still, I found no solution.</p>
  
  <p><strong>My question:</strong></p>
  
  <p>Is there any Regular Expression (or any other way) to find and extract a file name inside a larger string, based on the result of <code>Path.GetInvalidPathChars</code>?</p>