How to match an even number 1 and any number 0

I am having problems with this regex problem.

Language - {1,0}.

I need strings with an even number 1 and any number 0.

Examples of strings include:

110 101 11 0 empty set 1111 10101010101 
+6
source share
1 answer

^(0*10*1)*0*$ or ^(?:0*10*1)*0*$ if your groups without capture are supported by your regular expression engine.

It can also be "simplified" to ^((0*1){2})*0*$ , whatever you find more readable.

This corresponds to 1 pair and gaskets with any number of zeros if necessary. It does not match if the number 1 odd. It matches an empty string.

It does not use anything interesting, so it should work in most programming languages.

See in action regex101 .

+9
source

Source: https://habr.com/ru/post/974888/


All Articles