lokendra jayaswal
lokendra jayaswal

Reputation: 308

Get The Substring From Path Directory

I want the substring from the Directory path. Is there any query or Regex to do that. The path i have is like this:

E:\\Work\\Taxonomies\\012\\20110826\\20110826_final\\full_entry_point_2011-08-26.xsd

What i want is

20110826_final\\full_entry_point_2011-08-26.xsd

I want the path from the second last "\" i can split it in array but then i have to combine the last two values.

string[] path = value.Split('\\');
int length=path.Length;
if(length>1)
{
 string final = string.Concat(path[length-2],"\\", path[length-1]);
}

please guide me on this. Is there any other way to implement it.

Upvotes: 0

Views: 1033

Answers (3)

Dan Blanchard
Dan Blanchard

Reputation: 4054

An alternative approach to using regular expressions for this searching operation is to use the LastIndexOf method on the String class thus:

    static bool TryFindTrailingPart(string path, int numberOfSeparators, out string result)
    {
        var sep = Path.DirectorySeparatorChar;
        var lastSlash = path.Length;
        for(int i = 0; i < numberOfSeparators; i++)
        {
            lastSlash = path.LastIndexOf(sep, lastSlash - 1);
            if(lastSlash < 0)
            {
                result = null;
                return false;
            }
        }

        result = path.Substring(lastSlash + 1);
        return true;
    }

Although this code is substantially longer than a regular expression, it does have some advantages: it is somewhat more flexible than the regular expression approach (having parameterized the number of path separators to search for), and it executes substantially faster.

Running the above code in a tight loop with the OP's path expression takes around 200 milliseconds for 10000 iterations.

Using the regular expression approach given in Dmitry Dovgopoly's answer for the same number of iterations takes around 6890 milliseconds. Converting Dmitry's code to use a compiled regular expression (and excluding the time required to compile the expression) still takes around 3140 milliseconds.

Whether or not the speed and flexibility of this approach outweighs the increase in source code length largely depends upon your specific scenario: if you are processing (very!) large numbers of path strings, it might be useful. On the other hand, if you just need a "single use" piece of code to handle the path string processing problem, the regular expression approach is perhaps more appropriate.

Upvotes: 0

Dmitrii Dovgopolyi
Dmitrii Dovgopolyi

Reputation: 6301

String pattern = @"(.*\\)(.*\\.*$)";
String input = @"E:\Work\Taxonomies\012\20110826\20110826_final\full_entry_point_2011-08-26.xsd";
String result = Regex.Match(input, pattern).Groups[2].Value;

Edit

This regex divides string in two groups. First - all the symbols and \ it could take before second group. Second - all the remaining symbols which are 'text'\'text''endofstring'

Upvotes: 2

Loamhoof
Loamhoof

Reputation: 8293

As I don't know C#, you will maybe have a deal with the number of "\", but, if you want to do it with a regex:

/(?<=\\\\)[^\\]+\\\\[^\\]+$/

That is "one or more characters preceded by 2 backslashes, then 2 backslashes, then one or more characters until the end". When I mean character, I mean everything but a backslash.

Upvotes: 0

Related Questions