Reputation: 43
Given I have a sample string below:
"parentstring1[childstring1.childstring2.childstring3].parentstring2[childstring4.childstring5]"
How can I split the string above to produce an array with the values below from '.'
character and the '[' and ']' as delimiters
array[0] = "parentstring1[childstring1.childstring2.childstring3]"
array[1] = "parentstring2[childstring4.childstring5]"
Upvotes: 2
Views: 239
Reputation: 4410
You can use Regex like this:
[^\[\.]+\[[^\]]*\]
This means: anything except . and [, followed by anything inside [] symbols.
MatchCollection matches = Regex.Matches(source, "[^\[\.]+\[[^\]]*\]")
Upvotes: -1
Reputation: 5414
string text = "parentstring1[childstring1.childstring2.childstring3].parentstring2[childstring4.childstring5]";
string[] arr = Regex.Split(text, "(?<=\\]).");
foreach (var item in arr)
{
Console.WriteLine(item);
}
Upvotes: 1
Reputation: 784878
You can split using this regex:
\.(?![^\[]*\])
This will split on DOTs only when it is not followed by 0 or more non-[
characters and a ]
, thus making sure to split on DOTs that are outside [...]
.
Upvotes: 1