Solved

Regex...extract filename

Posted on 2003-12-10
8
1,857 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
Running ftp server and transfering data to the path using Windows Service 6 50
Selenium and Xpath 4 69
array not updating 8 42
Visual C# Windows Project - Clone Form 4 39
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
Suggested Courses

738 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