I want to split the following kind of string:
Parent/Child/Value [4za] AX/BY
and get create a String[]
out of it via:
String[] ArrayVar = Regex.Split(stringVar, "?");
which split the string by every /
before first appearance of [
.
So as result I would get
Array[0] => "Parent"
Array[1] => "Child"
Array[2] => "Value [4za] AX/BY"
strings could also have other formats like
Parent/Value [4za] AX/BY
Value [4za] AX/BY
How can I do this?
You could use normal string operations to do this. Just split the string on the first [
then split that accordingly. Then just add the end part of the string onto the last element:
string inputstring = "Parent/Child/Value [4za] AX/BY";
int index = inputstring.IndexOf('[');
string[] parts = inputstring.Substring(0, index).Split('/');
parts[parts.Length - 1] += inputstring.Substring(index);
Use negative lookbehind ((?<!...)
). The following regex means "/
not preceded by opening bracket":
(?<!\[.*)/
Demo
C# demo: https://dotnetfiddle.net/85S3cK
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