0

I want to write a regex which matches following scenarios,

10
1100
111000
11110000
// etc. consecutive 1 count should equal consecutive 0 count.

Is it possible to write a regex for this? I suppose I could write, 1{n}0{n}, but it didn't work here: https://regex101.com/

May be this is not doable with regex?

I can of course do this with a loop. But I want to know if this is possible using regex.

Thank you!!!!

Pubudu Dodangoda
  • 2,742
  • 2
  • 22
  • 38

1 Answers1

2

Theoretically the answer is no.

We can write regular expressions for strings that belong to regular languages. Your case belong to a context free language.

Luca Kiebel
  • 9,790
  • 7
  • 29
  • 44
Sreeragh A R
  • 2,871
  • 3
  • 27
  • 54