1

I have this format of string: 110001101 and I would like to split that string to string[] by grouped chunks of 1s or 0s.

So from that string I would get splitted string[] = { "11", "000", "11", "0", "1" };

Is it possible to achieve that with Regex, I don't know where to start? Or should I find another solution...

2 Answers 2

3

You can use a capturing mechanism and use Regex.Split with a mere (0+) regex like this:

var txt5 = "110001101";
var res5 = Regex.Split(txt5, @"(0+)").Where(p => !string.IsNullOrEmpty(p)).ToArray();

Result:

enter image description here

This will work as you do not have symbols other than 0 or 1 in your input string, and the captured text is also output as array elements. The LINQ code helps eliminate any unwelcome empty elements from the resulting array (as is the case with 0s only).

Sign up to request clarification or add additional context in comments.

Comments

3

So you want either consecutive zeros (0+) or consecutive ones (1+). Just iterate over the matches:

0+|1+

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.