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();
}
using
Regex.Matchesmight 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:The approach of the regular expression is to look for a sequence of:
[,], or/or
[something], where something is allowed to contain/