Solved

extracting file name from long string

Posted on 2011-02-24
8
299 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
[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
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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
 
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 63

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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
asp.net repeater 2 38
Example code 13 45
How to get the range for the percentage from user input. 4 27
XML Constructor Throwing Incorrectly Structured Document Error 4 25
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…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

710 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