Solved

extracting file name from long string

Posted on 2011-02-24
8
300 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

623 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