Javier García
Javier García

Reputation: 1114

Regex to extract paths both in Unix and Windows

I've written an application in .NET which gets a path from the executing assembly. It returns to me something like this:

In MacOS:

file:/RiderProjects/Aquamon/Aquamon/bin/Debug/netcoreapp2.0

In Windows

file:\C:\RiderProjects\Aquamon\Aquamon\bin\Debug\netcoreapp2.0

The thing is, to process those strings and obtain just the path I need to detach the file: part, so I wrote two different regexes:

(?<!file\/)\/+[\S\s]* for MacOS,

and

(?<!fil)[A-Za-z]:\\+[\S\s]* for Windows.

The issue is I would like to summarize both expressions in only one... but I can't seem to do it. The reason why I ended up with two was because in Windows I needed to extract that extra \ before the C:\..., which is not present in other systems.

Any help is appreciated! :)

Upvotes: 0

Views: 190

Answers (1)

NetMage
NetMage

Reputation: 26917

This appears to be what you want:

var pattern = @"(?<=file:\\?)[^\\].+";
var ans = Regex.Match(aPath, pattern).Value;

Upvotes: 1

Related Questions