简体   繁体   English

使用正则表达式匹配具有奇数个 0 和偶数个 1 的二进制字符串

[英]Using Regex To match Binary Strings that have an odd number of 0s and even number of 1s

[Redacted Info] [编辑信息]

Some valid inputs一些有效的输入

  • 101 101
  • 10001 10001
  • 00011 00011
  • 11000 11000
  • 0 0

Some invalid Inputs一些无效的输入

  • 11 11
  • 00 00
  • 10101 10101
  • 101101111 101101111

EDIT: For the people suggesting that ReGeX is not the way to go are absolutely correct, but for this question I need to use regex.编辑:对于那些认为 ReGeX 不是通往 go 的方法的人来说是绝对正确的,但是对于这个问题,我需要使用正则表达式。 Also, My definition of simpler is reducing the number of characters in the regex.(The minimum is about 22 characters long)另外,我对更简单的定义是减少正则表达式中的字符数。(最小值约为 22 个字符)

If you're determined to do this with regex, then for certain definitions of "simplify", this may fit the bill.如果您决定使用正则表达式来执行此操作,那么对于“简化”的某些定义,这可能符合要求。

(?=^0*((10*){2})*?$)(?=^1*(01*)((01*){2})*?$)^.*$

(?=                )                                assert that
   ^              $                                 between the start and end of the string
    0*                                              (consume leading zeros)
      (        )*?                                  there appears as many times as necessary
            {2}                                     two instances of 
       (10*)                                        a 1 followed by any number of 0s
                    (?=^1*     ((01*){2})*?$)       perform the same check as before
                          (01*)                     but require an extra 0 at the start

This relies on the {2} quantifier to demand a multiple of 2 for the numbers in question, and instead of validating the string all at once, performs 2 checks on the string: the first looks for an even number of 1s, and the second looks for an even number of 0s, plus an extra 0.这依赖于{2}量词来要求所讨论的数字是 2 的倍数,而不是一次验证字符串,而是对字符串执行 2 次检查:第一次查找偶数个 1,第二次查找查找偶数个 0,再加上一个额外的 0。

Demo演示

Using Counter from collections is another way to come at it.使用collections中的Counter是另一种方法。 It makes it easy to look for odd and even sequences as well as ensuring that the string has only 1s and 0s.它可以很容易地查找奇数和偶数序列,并确保字符串只有 1 和 0。

from collections import Counter

def is_valid_binary_string(test_string):
    c = Counter(test_string)
    
    # Not valid if anything other than 1s and 0s
    if set(c.keys()) - {"0", "1"}:
        return False
    
    # Valid only if even number of 1s and odd number of 0s
    return c["1"] % 2 == 0 and c["0"] % 2 == 1

You do not need regex for this.您不需要regex Its super simple to achieve this using string.count() and checking if the response is odd or even.使用string.count()并检查响应是奇数还是偶数来实现这一点非常简单。

testcases = ['101', '10001', '00011', '11000', '0', '11', '00', '10101', '101101111']
for string in testcases:
    print(string, 'Valid' if string.count('0') % 2 == 1 and string.count('1') % 2 == 0 else 'Invalid')

Outputs输出

101 Valid
10001 Valid
00011 Valid
11000 Valid
0 Valid
11 Invalid
00 Invalid
10101 Invalid
101101111 Invalid

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

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