Loading ...

Regex for extracting various parts of a network filepath

Who is online?  0 guests and 0 members
home  »  forums   »  asp.net topics   »  getting started / general asp.net   » Regex for extracting various parts of a network filepath

Regex for extracting various parts of a network filepath

Posts under the topic: Regex for extracting various parts of a network filepath

Posted: 6/22/2011

Lurker 30  points  Lurker
  • Joined on: 10/13/2010
  • Posts: 6

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.csv
Regex regParse = new Regex("(?<Dir>C:+.)(?<RequestType>.[^AFSR]+.)(?<RequestID>.+)(?<Ext>.csv+)");

ResponseR1234567890.csv
Regex regParse = new Regex("(?<RequestType>.[^AFSR]+.)(?<RequestID>.+)(?<Ext>.csv+)");

Any help on this is appreciated.

Thanks


tags regex, filepath

Posted: 6/22/2011

Starter 727  points  Starter
  • Joined on: 6/6/2011
  • Posts: 74
  Answered

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

 


tags FileName

Posted: 6/22/2011

Guru 16813  points  Guru
  • Joined on: 4/19/2009
  • Posts: 490
  Answered

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

Lurker 30  points  Lurker
  • Joined on: 10/13/2010
  • Posts: 6

Thanks for the responses Guys.  Appreciated,


Page 1 of 1 (4 items)