简体   繁体   English

可以被4整除的二进制数的正则表达式

[英]Regular expression for binary numbers which are divisible by 4

I want to create a regular expression to match the following: 我想创建一个正则表达式来匹配以下内容:

L : the set of all bit strings (ie strings over alphabet {0,1} ) that are divisible by 4 L :可被4整除的所有位字符串(即字母{0,1}字符串)的集合

If a binary is divisible by four, the last two bits are zero. 如果二进制可被四整除,则最后两位为零。 So you can use this Regex to match: 因此,您可以使用此正则表达式进行匹配:

/.+00$/

or, if you want to check that it is indeed a binary number (only zeros and ones), you can use: 或者,如果您要检查它确实是一个二进制数(仅是零和一),则可以使用:

/[01]+00$/

If you also want to match 0 and 00 : 如果您还想匹配000

/^(00?|[01]+00)$/

if you don't want to match all zeros, you can use: 如果您不想匹配所有零,则可以使用:

/(?=1)[01]+00$/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM