Solved

extracting file name from long string

Posted on 2011-02-24
8
291 Views
Last Modified: 2012-05-11
What's best way in regex to extract the file name from the below format of a string


2011-02-22 17:49:42 ....A         7114         1088  ErrE\CC\RM-004_2011.05_01.02_49u.2011.04_rnd.logform3.fps

Open in new window

0
Comment
Question by:NewtonianB
8 Comments
 
LVL 3

Accepted Solution

by:
4runnerfun earned 125 total points
ID: 34971514
Without RegEx, you could split your string with "\" and then use the last index which would contain your filename.
0
 
LVL 33

Assisted Solution

by:Todd Gerbert
Todd Gerbert earned 125 total points
ID: 34971545
That text looks like output of a "dir" or something - which means the filename should always start at the nth position, so you could use String.Substring. Or if the file name never contains spaces, find start of file name with LastIndexOf.
0
 

Author Comment

by:NewtonianB
ID: 34971813
4runnerfun: sometimes there is no path it is just the file name thats why regex provides most flexibility for me

tgerbert: there could be spaces in the path
0
 

Author Comment

by:NewtonianB
ID: 34972001
basically all i know is that it will end with  .fps and I need to find the filename (lets actually assume there could be no space in the path or filename)

wether its

2011-02-22 17:49:42 ....A         7114         1088  ErrE\CC\RM-004_2011.05_01.02_49u.2011.04_rnd.logform3.fps


or


2011-02-22 17:49:42 ....A         7114         1088  RM-004_2011.05_01.02_49u.2011.04_rnd.logform3.fps


I need it to output
RM-004_2011.05_01.02_49u.2011.04_rnd.logform3.fps
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 34972626
Assuming the path/file always starts at the 54th character (and I would guess that it does):
string input = @"2011-02-22 17:49:42 ....A         7114         1088  ErrE\CC\RM-004_2011.05_01.02_49u.2011.04_rnd.logform3.fps";
string fileName = System.IO.Path.GetFileName(input.Substring(53));

Open in new window


Or, to go from the right-most space to the end of the line:
string input = @"2011-02-22 17:49:42 ....A         7114         1088  ErrE\CC\RM-004_2011.05_01.02_49u.2011.04_rnd.logform3.fps";
string fileName = System.IO.Path.GetFileName(input.Substring(input.LastIndexOf(' ') + 1));

Open in new window

0
 
LVL 62

Assisted Solution

by:Fernando Soto
Fernando Soto earned 125 total points
ID: 34972720
Hi NewtonianB;

The code in the code snippet should do what you need using Regular Expression.

using System.Text.RegularExpressions;


string testInput = @"2011-02-22 17:49:42 ....A         7114         1088  ErrE\CC\RM-004_2011.05_01.02_49u.2011.04_rnd.logform3.fps" + "\n" +
    @"2011-02-22 17:49:42 ....A         7114         1088  RM-004_2011.05_01.02_49u.2011.04_rnd.logform3.fps";

string pattern = @"(\b[^\s]+\.fps\b)";

MatchCollection mc = Regex.Matches(testInput, pattern);

foreach(Match m in mc)
{
    Console.WriteLine(m.Value);
}

Open in new window


Fernando
0
 
LVL 11

Assisted Solution

by:Sudhakar Pulivarthi
Sudhakar Pulivarthi earned 125 total points
ID: 34978604
Hi
FernandoSoto: solution was good to regex, but it gives the file path when there is any path provided in the source string so its good to add one more line of code to get file name always.

string fileName = null;
string pattern = @"(\b[^\s]+\.fps\b)";
string b=@"2011-02-22 17:49:42 ....A         7114         1088  ErrE\C C\RM-004_2011.05_01.02_49u.2011.04_rnd.logform3.fps";
 MatchCollection mc = Regex.Matches(b, pattern);
if (mc.Count == 1)
{
fileName = Path.GetFileName(mc[0].Value); // This will return the file name only even when path is matched
}
0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 34982482
I think you'd want that regular expression to end with "$", e.g. "\b\S+\.fps$", it is possible for a character matching word-boundary to appear in the middle of the filename.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Article by: Ivo
Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

707 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

13 Experts available now in Live!

Get 1:1 Help Now