Regular expression to trim a string

495 views Asked by At

In my application, i am trying to get the name of a file, from a string retrieved from a 'content-header' tag from a server. The filename looks like \"uploads/2014/03/filename.zip\" (quotations included in value).

I have tried using Path.GetFileName(string); to get just the file name but it throws an exception stating that there are illegal characters in the path.

What should i use to get just filename.zip returned? is a regex the best way to trim this string off or is there a better one?

the \"uploads/2014/03/ part will always be the same length. The filename.zip can be any filename and extension, im just using that as an example. But the numbers may vary. It sounds like a job for a regex to me, but i have no idea how to use regular expressions.

1

There are 1 answers

0
Selman Genç On BEST ANSWER

You can try something like this:

var inputString = @"\""uploads/2014/03/filename.zip\""";

var result = inputString.Trim('\\', '"').Split('/')[3];

This should work if the format is always like \"uploads/someNumber/someOtherNumber/filename\".In order to make it more safe you might want to use Enumerable.Last method after Split:

var result = inputString.Trim('\\', '"').Split('/').Last();