I am trying to split a string representing XPath, for example:
string myPath = "/myns:Node1/myns:Node2[./myns:Node3=123456]/myns:Node4";
I need to divide by "/" ("/" is excluded from the results, as with normal line separation), if "/" is not in "[...]" (where "/" will not be separated, and also included in result).
So, that normal string[] result = myPath.Split("/".ToCharArray()) gets me:
result[0]: //Empty string, this is ok result[1]: myns:Node1 result[2]: myns:Node2[. result[3]: myns:Node3=123456] result[4]: myns:Node4
results[2] and result[3] should be combined, and I have to finish:
result[0]: //Empty string, this is ok result[1]: myns:Node1 result[2]: myns:Node2[./myns:Node3=123456] result[3]: myns:Node4
Since I'm not sure about the regex, I tried to manually recombine the results into a new array after splitting, but it worries me that although it is trivial to make it work for this example, the regex seems to be the best option when I get more complex xpaths .
For the record, I considered the following questions:
Regular Expression Separating String
C # Regex Split - commas outside quotes
Separate a line that has spaces if they are not enclosed in "quotes" quot ;?
While they should be sufficient to help with my problem, I come across several problems / confusing aspects that prevent them from helping me.
In the first two links, as a newbie in regex, it's hard for me to interpret and learn them. They are looking for quotes that look the same between left and right pairs, so translating it into [and] is confusing to me, and trial and error doesn't teach me anything, rather, it just upsets me more. I can understand a pretty basic regex, but what these answers do is a bit more than what I understand now, even with the explanation in the first link. In the third link, I will not have access to LINQ, since the code will be used in the old version of .NET.