I am stuck with a problem that challenges me to create a regex for binary numbers (containing 0and 1s). But the string should only match if the binary number contains a maximum of five 1s. How to limit the appearance of a character in a regular expression?
0
1
Examples:
01101101
01111100
10110011
01111110
11111110
^0*(?:10*){,5}$
Essentially, this matches any combination '1'and '0', but assumes that a substring containing a single character '1'will be executed a maximum of five times.
'1'
'0'
Try it here: https://regex101.com/r/JKV1Uk/2
Explanation:
^ matches the beginning of a line
^
0* '0' s
0*
(?:10*){,5} 5 '1',
(?:10*){,5}
$
lookaheads 8 1 0, 1 5 , :
^(?=[01]{8}$)(?!(?:0*1){6,})[01]+$
(?=
[01]{8}$
)
(?!
(?:0*1){6,}
0*1
[01]+$
Source: https://habr.com/ru/post/1695294/More articles:https://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1695289/populate-dicts-of-number-of-ones-to-the-left-and-right-of-bitstring&usg=ALkJrhhWj0lH0AbUY3VrGdVjL3C4sBj2GQHow to specify list type in function parameter? - f #Unexpected behavior in simple pointer arithmetic in kernel space C code - cКак зарегистрировать справку/reset/настройки Скорректированная (не активная) функция Azure Functions Bot? - c#Linking 32-bit and 64-bit code together in one binary - gccHow to change the step size used by matplotlib when building timestamp objects? - pythoncontrol the number of x ticks in the gun - matplotlibHow to configure script compatibility mode? - compatibilityHow can I create register pointer events of the -ms-thumb element on a basic range input in two input overlays in IE? - csspassing rvalue to the non-ref parameter, why can't the compiler delete the copy? - c ++All Articles