Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex:长度是3的倍数的二进制字符串?_Regex - Fatal编程技术网

Regex:长度是3的倍数的二进制字符串?

Regex:长度是3的倍数的二进制字符串?,regex,Regex,如何为二进制字符串编写正则表达式,使其长度为3的倍数,并且必须包含空字符串。 比如说 这是真的 0101为假。以下操作应有效: ^(?:[01]{3})*$ 编辑:用于优化的非捕获组。将正则表达式包装在^$中,以便它对整个字符串执行匹配 ^([01]{3})*$ 以下是我的建议: ^(?:[01]{3})*$ 它匹配您需要的模式,但不捕获括号中的组 说明: ^ // matches beginning of the string (?: // opens a non-capturi

如何为二进制字符串编写正则表达式,使其长度为3的倍数,并且必须包含空字符串。 比如说 这是真的
0101为假。

以下操作应有效:

^(?:[01]{3})*$

编辑:用于优化的非捕获组。

将正则表达式包装在
^
$
中,以便它对整个字符串执行匹配

^([01]{3})*$

以下是我的建议:

^(?:[01]{3})*$
它匹配您需要的模式,但不捕获括号中的组

说明:

^ // matches beginning of the string
    (?: // opens a non-capturing group
        [01] // a symbol class, which could only contain 0's or 1's
        {3} // repeat exactly three times
    ) // closes the previously opened group
    * // repeat [0, infinity] times
$ // matches the end of the string

漂亮紧凑,我注意到了,喜欢你的正则表达式风格。向上投票:)