Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

extracting file name from long string

Posted on 2011-02-24
8
Medium Priority
?
301 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 500 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 500 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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 64

Assisted Solution

by:Fernando Soto
Fernando Soto earned 500 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 500 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

721 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