How to use Path.GetInvalidPathChars to find a file name inside a larger string?

Viewed 2595

My goal is to find a file name ("MyFile.txt") inside a larger string. I.e.:

Some text before MyFile.txt some other text after

Currently I'm successfully using a Regular Expression with a character class of something like the following (simplified):

[\w\.\-]

This works fine, until the file contains other characters that are outside the \w group, e.g. an em dash: "My—File.txt".

My approach:

The method Path.GetInvalidPathChars returns an array of invalid characters. I've tried to use this method. Unfortunately, I found no way of "converting" this to be useful inside a Regular Expression.

I'm aware of

Still, I found no solution.

My question:

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 Path.GetInvalidPathChars?

2 Answers
Related