|
-
Jun 11th, 2013, 10:51 AM
#1
Thread Starter
Frenzied Member
[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.
-
Jun 11th, 2013, 12:39 PM
#2
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:
string text = "3;#bctest/Lists/Test List 1/Folder 1\\"; //split it by the forward slash / string[] folders = text.Split('/'); //show the last element of the array and replace the backslash with blank MessageBox.Show(folders[folders.Length - 1].Replace("\\", ""));
-
Jun 11th, 2013, 01:08 PM
#3
Thread Starter
Frenzied Member
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?
-
Jun 11th, 2013, 02:45 PM
#4
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:
string text = @"3;#bctest/Lists/Test List 1/Folder 1\"; int start = text.LastIndexOf('/') + 1; int end = text.LastIndexOf('\\'); string test = text.Substring(start, end - start);
Runs 1,000,000 times in 0.074 seconds
-
Aug 4th, 2013, 12:24 PM
#5
Thread Starter
Frenzied Member
Re: Regex help
That seems to do the trick. Thanks!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|