?
Solved

In need of a regular expression for file paths

Posted on 2007-10-08
7
Medium Priority
?
187 Views
Last Modified: 2010-04-15
Please help me find a regular expression to determine the root directory of a windows file share.  I am using C# for the first time and am having a bit of trouble with this.

Also, if you think I may be going about this the wrong way I would much appreciate any suggestions.

Here are some examples:
String: "\\server01.fs.domain.net\Shared$\documents\2005 presentations\last quarter"
should return: "\\server01.fs.domain.net\Shared$\documents"

String: "\\server01.fs.domain.net\Shared$\documents\2005 presentations\Presentation1.ppt"
should return: "\\server01.fs.domain.net\Shared$\documents"

and of course: "\\server01.fs.domain.net\Shared$\documents"
should return: "\\server01.fs.domain.net\Shared$\documents"

Thanks for the help!
0
Comment
Question by:ynotravid
  • 3
  • 3
7 Comments
 
LVL 17

Accepted Solution

by:
mreuring earned 2000 total points
ID: 20037973
"\\(\\[^\\]+){3}"

This would give you the first three instances of \..... plus the leading slash. Which is pattern I made up out of your example. Allas I an somewhat unfamiliar with C# and wouldn't quite be able to tell you how to use this regular expression :(
0
 
LVL 17

Expert Comment

by:NicksonKoh
ID: 20038104
Hi ynotravid,

Here's the C# code.

using System.Text.ReularExpressions;

class Test
{
static void Main()
  {
  string text = "\\server01.fs.domain.net\Shared$\documents\2005 presentations\last quarter";
  string regex = ".+(?=\\)";
  string replacement = "$1";
  }
}

string result =  Regex.Replace(text, regex, replacement, RegexOptions.IgnoreCase);

Cheers,
NicksonKoh
0
 
LVL 17

Expert Comment

by:NicksonKoh
ID: 20038106
Typo :p

using System.Text.ReularExpressions;

class Test
{
static void Main()
  {
  string text = "\\server01.fs.domain.net\Shared$\documents\2005 presentations\last quarter";
  string regex = ".+(?=\\)";
  string replacement = "$1";

  string result =  Regex.Replace(text, regex, replacement, RegexOptions.IgnoreCase);
  }
}

Cheers,
NicksonKoh
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 17

Expert Comment

by:mreuring
ID: 20038249
I think there's another typo, I think the first line should probably be:
using System.Text.RegularExpressions;

Also, your regular expressions will only not match on the last sub-dir (unless I'm completely misreading it) it would only remove the last subdir, which doesn't match ynotravid's explanation. And, since there's no grouping, $1 should result in an empty string, unless C# behaves completely different from all other regular expression implementations. As a result, I think your method would result in an empty-string, or perhaps in '\'.

Instead of search and replacing everything it'd be a lot faster to go streight to a match, I just googled on regular expressions in C# cause I don't know the language myself (I usualy do Java and JavaScript) and it seems simple enough, based on http://www.windowsdevcenter.com/pub/a/oreilly/windows/news/csharp_0101.html:
Match m = Regex.Match("\\server01.fs.domain.net\Shared$\documents\2005 presentations\last quarter", "\\(\\[^\\]+){3}");
if (m.Succes) {
  String result = m.ToString();
}

Warning though, this code is untested!! (as seem to be the previous two posts) You should probably read the mentioned article if you need more background info.
0
 
LVL 17

Expert Comment

by:NicksonKoh
ID: 20038293
Thx for pointing out the typos; I did also miss the brackets in the regex. It should be "(.+)(?=\\)";
And the expression is indeed not as required as it will match to the last folder. I've corrected as follows

using System.Text.RegularExpressions;

class Test
{
static void Main()
  {
  string text = "\\server01.fs.domain.net\Shared$\documents\2005 presentations\last quarter";
  string regex = ".+\\documents";
  string replacement = "$1";

  string result =  Regex.Replace(text, regex, replacement, RegexOptions.IgnoreCase);
  }
}

Actually, both ur expression and my expression has it assumptions. Yours assume a fix 3 folder root setup while the above expression I've provided assume the root folder is documents.
0
 
LVL 17

Expert Comment

by:mreuring
ID: 20038460
That's the problem with regular expressions, you always have to make an assumptions :)
0
 

Author Comment

by:ynotravid
ID: 20038631
Thanks so much for the responses!  
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

850 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