Solved

Regex...extract filename

Posted on 2003-12-10
8
1,856 Views
Last Modified: 2009-07-29
I always think I understand regex's until I go to use them. I'm using C# and .NET Framework.   Let's say I have the following string:

"C:\\MsDev\\Sharp\\bin\\Debug\\Control.dll`~`3`~`0`~`"    notice that `~` is my delimiter

I want to extract the filename from the string using regex.

I think the following snippet should work, but it is assigning 'filename' with the entire string, not just the filename.  Any ideas??  Thanks in advance.

//Get the filename from string using regex...
string filename = (Regex.Replace(dataString, "^(.*)(`~`)?", "$1")).ToString();
0
Comment
Question by:GorGor1
  • 3
  • 3
  • 2
8 Comments
 
LVL 9

Expert Comment

by:msdixon
ID: 9914961
System.IO.Path.GetFileName(file); // where file is the full path and file name
0
 
LVL 1

Author Comment

by:GorGor1
ID: 9914994
How does that work??  How is that going to extract the filename from my `~` delimited string??
0
 
LVL 20

Accepted Solution

by:
TheAvenger earned 50 total points
ID: 9915102
Is it obligatory to use regex? You can use this:

int delPos = dataString.IndexOf ("'~'");
string filename = dataString.Substring (0, delPos);
filename = filename.Substring (filename.LastIndexOf ("\\") + 1);
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 1

Author Comment

by:GorGor1
ID: 9915143
I'd much rather use regex.  The string is actually much much longer than what I posted here and I'm using regex to remove and read elements of the string, etc.

Long story short, using Regex.Replace(...) makes the most sense in my particular case.
0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 9915668
OK, this one works in your case:

string dataString = "C:\\MsDev\\Sharp\\bin\\Debug\\Control.dll'~'3'~'0'~'";
string pattern = "(?:[\\w\\\\:]*)(?:\\\\)([^\\\\']*).*";
string filename = (Regex.Replace(dataString, pattern, "$1")).ToString();
MessageBox.Show (filename);
0
 
LVL 1

Author Comment

by:GorGor1
ID: 9916659
Thanks, it works great!  Turns out that I like your other solution better.  It's definitely a much cleaner approach.  Thanks again.
0
 
LVL 9

Expert Comment

by:msdixon
ID: 9917103
so how is that any easier than using the Path.GetFileName method? it seems like a lot more work to me.
0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 9918693
He has the file path in a string variable among other data and wants to extract it. Path.GetFileName will give you the path of a pysically existing file which is absolutely different
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
converto datetime always changes format 9 42
Cant save 3D 4 23
transaction in asp.net, sql server 6 43
custom authorization on controller action and HTML in asp.net mvc 1 30
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question