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$\do cuments\20 05 presentations\last quarter"
should return: "\\server01.fs.domain.net\ Shared$\do cuments"
String: "\\server01.fs.domain.net\ Shared$\do cuments\20 05 presentations\Presentation 1.ppt"
should return: "\\server01.fs.domain.net\ Shared$\do cuments"
and of course: "\\server01.fs.domain.net\ Shared$\do cuments"
should return: "\\server01.fs.domain.net\ Shared$\do cuments"
Thanks for the help!
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\
should return: "\\server01.fs.domain.net\
String: "\\server01.fs.domain.net\
should return: "\\server01.fs.domain.net\
and of course: "\\server01.fs.domain.net\
should return: "\\server01.fs.domain.net\
Thanks for the help!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Typo :p
using System.Text.ReularExpressi ons;
class Test
{
static void Main()
{
string text = "\\server01.fs.domain.net\ Shared$\do cuments\20 05 presentations\last quarter";
string regex = ".+(?=\\)";
string replacement = "$1";
string result = Regex.Replace(text, regex, replacement, RegexOptions.IgnoreCase);
}
}
Cheers,
NicksonKoh
using System.Text.ReularExpressi
class Test
{
static void Main()
{
string text = "\\server01.fs.domain.net\
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.RegularExpress ions;
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.ne t\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.
using System.Text.RegularExpress
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
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.RegularExpress ions;
class Test
{
static void Main()
{
string text = "\\server01.fs.domain.net\ Shared$\do cuments\20 05 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.
And the expression is indeed not as required as it will match to the last folder. I've corrected as follows
using System.Text.RegularExpress
class Test
{
static void Main()
{
string text = "\\server01.fs.domain.net\
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 :)
ASKER
Thanks so much for the responses!
Here's the C# code.
using System.Text.ReularExpressi
class Test
{
static void Main()
{
string text = "\\server01.fs.domain.net\
string regex = ".+(?=\\)";
string replacement = "$1";
}
}
string result = Regex.Replace(text, regex, replacement, RegexOptions.IgnoreCase);
Cheers,
NicksonKoh