Link to home
Start Free TrialLog in
Avatar of ynotravid
ynotravid

asked on

In need of a regular expression for file paths

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!
ASKER CERTIFIED SOLUTION
Avatar of mreuring
mreuring
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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
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.
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.
That's the problem with regular expressions, you always have to make an assumptions :)
Avatar of ynotravid
ynotravid

ASKER

Thanks so much for the responses!