Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get forward slash except within square brackets

Tags:

c#

regex

Example text (I've highlighted the desired /'s):

pair[@Key=2]/Items/Item[Person/Name='Martin']/Date

I'm trying to get each forward slash that isn't within a square bracket, anyone slick with Regex able to help me on this? The use would be:

string[] result = Regex.Split(text, pattern);

I've done it with this code, but was wondering if there was a simpler Regex that would do the same thing:

private string[] Split()
{
    List<string> list = new List<string>();
    int pos = 0, i = 0;
    bool within = false;
    Func<string> add = () => Format.Substring(pos, i - pos);
    //string a;
    for (; i < Format.Length; i++)
    {
        //a = add();
        char c = Format[i];
        switch (c)
        {
            case '/':
                if (!within)
                {
                    list.Add(add());
                    pos = i + 1;
                }
                break;
            case '[':
                within = true;
                break;
            case ']':
                within = false;
                break;
        }
    }
    list.Add(add());
    return list.Where(s => !string.IsNullOrEmpty(s)).ToArray();
}
like image 255
Chuck Savage Avatar asked Oct 07 '22 20:10

Chuck Savage


2 Answers

using Regex.Matches might be a better approach than trying to use split. Instead of specifying a pattern to split on, you try to match your desired outputs. This works in basic cases:

string[] FancySplit(string input) {
    return Regex.Matches(input, @"([^/\[\]]|\[[^]]*\])+")
        .Cast<Match>()
        .Select(m => m.Value)
        .ToArray();
}

The approach of the regular expression is to look for a sequence of:

  • characters that are not [, ], or /
    or
  • A sequence of the form [ something ], where something is allowed to contain /
like image 183
recursive Avatar answered Oct 10 '22 09:10

recursive


This will not work for general case, but it will do most practical cases right:

(?<!\[[^]]+)/

This expression uses a negative lookbehind to match a forward slash unless it is preceded by a square bracket followed by a sequence of characters other than a closing square bracket.

like image 24
Sergey Kalinichenko Avatar answered Oct 10 '22 09:10

Sergey Kalinichenko