Solved

Regex...extract filename

Posted on 2003-12-10
8
1,849 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

760 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now