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.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:
[
, ]
, or /
[
something ]
, where something is allowed to contain /
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With