C# - Regex for file paths e.g. C:\test\test.exe

Viewed 70534

I am currently looking for a regex that can help validate a file path e.g.:

C:\test\test2\test.exe
10 Answers

Try this one for Windows and Linux support: ((?:[a-zA-Z]\:){0,1}(?:[\\/][\w.]+){1,})

I know this is really old... but expanding on @agent-j's response I've added named groups, and support for period characters.

^(?<ParentPath>(?:[a-zA-Z]\:|\\\\[\w\s\.]+\\[\w\s\.$]+)\\(?:[\w\s\.]+\\)*)(?<BaseName>[\w\s\.]*?)$

I've saved this at Regexr

Related