Using grep, I am trying to match lines which are comprised of two characters, one followed repeated followed by the other, but only match when the number of first character occurances is equal to the occurrences of the second character.
As an example, imagine that I can only match two characters like '0' and '1'. Now imagine that if there are n '0' characters then there must be n '1' characters following directly afterward. For example:
- ''
- '0011'
- '000111'
- '00000000001111111111'
would all match. But:
- '011'
- '1100'
- '110001'
wouldn't match.
I've been playing around with capture groups and trawling through perldoc for more info on grep -P but haven't found any leads to solve my problem - with grep at least.
How could I make a grep command to match strings given these constraints?
EDIT:
- In this example, the 0s should come before the 1s as per the restriction "following directly afterward"
- The empty string should also be a match case because by the example restrictions, when there are n 0s there should be n 1s, so with zero 0s there should be zero 1s.