I’m struggling to set up an expression that works with all my testing binary strings.
I’ve had a few attempts along the lines of (0|1)*1(0|1)*1(0|1)* | 1*0?1*0?1*
and a few other obviously wrong attempts. A few binary strings I have written have failed so I’m unsure regarding whether its worthwhile. Any assistance is greatly appreciated.
looomp is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
4
The “at least two 1s” condition can be implemented with a positive lookahead pattern that matches when there are two 1s:
^(?=(?:.*1){2})
And the “at most two 0s” condition can be implemented with a negative lookahead pattern that fails a match when there are 3 0s:
^(?!(?:.*0){3})
So to match binary strings where either of the conditions is true, combine the two lookahead patterns in an alternation pattern:
^(?:(?=(?:.*1){2})|(?!(?:.*0){3}))[01]+$
Demo: https://regex101.com/r/bitdtJ/1