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