Judging by your posting history, I'll guess you're talking about C# (.NET) regexes. In that case, this should work:
Regex.Split(target, @"(?<!\[)/");
This assumes every non-delimiter /
is immediately preceded by a left square bracket, as in your sample data.
You should always specify which regex flavor you're working with. This technique, for example, requires a flavor that supports lookbehinds. Off the top of my head, that includes Perl, PHP, Python and Java, but not JavaScript.
EDIT: Here's a demonstration in Java:
public class Test
{
public static void main(String[] args)
{
String str = "/div1/div2[/div3[/div4]]/div5/div6[/div7]";
String[] parts = str.split("(?<!\\[)/");
for (String s : parts)
{
System.out.println(s);
}
}
}
output:
div1
div2[/div3[/div4]]
div5
div6[/div7]
Of course, I'm relying on some simplifying assumptions here. I trust you'll let me know if any of my assumptions are wrong, Mike. :)
EDIT: Still waiting on a ruling from Mike about the assumptions, but Chris Lutz brought up a good point in his comment to 280Z28. At the root level in the sample string, there are two places where you see two contiguous /divN
tokens, but at every other level the tokens are always isolated from each other by square brackets. My solution, like 280Z28's, assumes that will always be true, but what if the data looked like this?
/div1/div2[/div3/div8[/div4]/div9]/div5/div6[/div7]
Now we've got two places where a non-delimiter slash is not preceded by a left square bracket, but the basic idea is. Starting from any point the root level, if you scan forward looking for square brackets, the first one you find will always be a left (or opening) bracket. If you scan backward, you'll always find a right (or closing) bracket first. If both of those conditions are not true, you're not at the root level. Translating that to lookarounds, you get this:
/(?![^\[\]]*\])(?<!\[[^\[\]]*)
I know it's getting pretty gnarly, but I'll this take over that godawful recursion stuff any day of the week. ;) Another nice thing is that you don't have to know anything about the tokens except that they start with slashes and don't contain any square brackets. By the way, this regex contains a lookbehind that can match any number of characters; the list of regex flavors that support that is very short indeed, but .NET can do it.