views:

25

answers:

1

Hi all, this code was working fine till this morning, can anyone spot my mistake? probably really silly but it has me stumped!

i use a form to submit a file (field name 'fileUpEx'), and then i wrote a class to upload it (like i said, it's been working for ages).... (if i write 'filepath' to the page it is 'Test copy.pdf') My class returns 'no groups'!!! Very odd, can anyone please help?

string filepath = fileUpEx.PostedFile.FileName;
string pat = @"\\(?:.+)\\(.+)\.(.+)";
Regex r = new Regex(pat);
Match m = r.Match(filepath);
if (m.Groups[0].Captures.Count != 0)
{ 
   //blaa blaa blaa
}
else 
{
   return "no Groups";
}

Thanks in advance, Vauneen

A: 

Your regular expression requires that the file path contains a backslash which it doesn't. You could perhaps make that part optional, for example:

@"(?:\\.+\\)?(.+)\.(.+)"

Alternatively you could use the methods available in System.IO.Path:

string extension = Path.GetExtension(filePath);
string filename = Path.GetFilenameWithoutExtension(filePath);
Mark Byers
This worked perfectly! Thanks Millions Mark!
Vauneen