I want to split on '0' and '1' while keeping those characters in the split result. How can I do this in C#?
e.g.
"012345" => 0,1,2345
I tried
Regex.Split(number, @"(?=[01])");
but I get "", 0, 12345 as the result
The following seems to work, except for "" in between splits.
Regex.Split(number, @"(0|1)");
You could use LINQ to simply exclude the empty elements using the regex pattern you mentioned in your post:
This could also work. I’d like to see a more elegant approach, which I feel is what you are seeking, but it gets the job done.