Posted: 6/22/2011
Hi
I have a filepath (list of up to 5 as below), and I need to extract the 'Response[R,F,C,L,A]' part, and any numeric value (can be 1 to N) that follows the 'Response[R,F,C,L,A]' part, e.g. for \\servername\directoty\subdirectory\ResponseR12345.csv, I would want to extract ResponseR and 12345\\servername\directoty\subdirectory\ResponseR12345.csv\\servername\directoty\subdirectory\ResponseF12345.csv\\servername\directoty\subdirectory\ResponseC12345.csv\\servername\directoty\subdirectory\ResponseL12345.csv\\servername\directoty\subdirectory\ResponseA12345.csv
I got this to work for a local file path, and the file name on its own (using the regex below), but not for a variable length file share (e.g. above). C:\\ResponseR1234567890.csvRegex regParse = new Regex("(?<Dir>C:+.)(?<RequestType>.[^AFSR]+.)(?<RequestID>.+)(?<Ext>.csv+)");
ResponseR1234567890.csvRegex regParse = new Regex("(?<RequestType>.[^AFSR]+.)(?<RequestID>.+)(?<Ext>.csv+)");
Any help on this is appreciated.
Thanks
Hi,
I think that you use complex solution for your problem.
Try to get only the file name with or without file extension, and use your regex to extract what you need. I'll recomend to use:
string path = "\\servername\directoty\subdirectory\ResponseR12345.csv"; string fileName = Path.GetFileNameWithoutExtension(path); --> fileName = "ResponseR12345"
The output will be file name without extension, nevermind its local path or some relative path.
When you get only the file name, user your regex.
Best Regards,
Gjorgji
Yes, I will go with the same what Gjorgji suggested. You can refer this blog too http://codeasp.net/blogs/raghav_khunger/microsoft-net/1245/get-filename-without-extension-in-c
Posted: 6/23/2011
Thanks for the responses Guys. Appreciated,