Results 1 to 5 of 5

Thread: [RESOLVED] Regex help

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091

    Resolved [RESOLVED] Regex help

    I've got a string such as:

    "3;#bctest/Lists/Test List 1/Folder 1\"

    I want to use Regex to extract that last folder, in this case: "Folder 1"

    So essentially, I need to always extract the string immediately after the last forward slash (/), without including the last backslash (\). It's important to note that the string can contain any number of forward slashes, but I'm only interested in the last forward slash.

    Any help would be appreciated.

    Visual Studio 2010

  2. #2
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: Regex help

    I typically try to avoid regex in every case possible.

    Is there a reason you wouldn't do something like this?

    C# Code:
    1. string text = "3;#bctest/Lists/Test List 1/Folder 1\\";
    2.  
    3. //split it by the forward slash /
    4. string[] folders = text.Split('/');
    5.  
    6. //show the last element of the array and replace the backslash with blank
    7. MessageBox.Show(folders[folders.Length - 1].Replace("\\", ""));

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091

    Re: Regex help

    The issue is efficiency. What I didn't say is that this is in a loop with potentially hundreds or thousands of these. I need the most efficient method possible. What is more efficient? RegEx or the example you provided?

    Visual Studio 2010

  4. #4
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: Regex help

    Regex is notoriously slow. Unfortunately I am not that good at regex, but I did tweak my example a bit.

    This example

    C# Code:
    1. string text = @"3;#bctest/Lists/Test List 1/Folder 1\";
    2.  
    3. int start = text.LastIndexOf('/') + 1;
    4. int end = text.LastIndexOf('\\');
    5.  
    6. string test = text.Substring(start, end - start);


    Runs 1,000,000 times in 0.074 seconds

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091

    Re: Regex help

    That seems to do the trick. Thanks!

    Visual Studio 2010

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width