正则表达式用于检查每个子字符串

时间:2017-02-07 10:54:34

标签: regex finite-automata deterministic

我在最后一个学期的考试中遇到了这个问题,不幸的是我无法解决它并试了好几天而且没有运气。

条件 - 对于长度为n的字符串中长度为4的每个子字符串,写一个RE来强制规则 - 必须正好有三个1。

我的解决方案看起来像 (1 + 11 + 111 +€)(0111)*(0 +€)。

但这显然是错误的,字符串11011也是一个有效的解决方案。

更新 - 我的新解决方案是(1 + 11 + 111 +€)(0111)*(0 + 01 + 011 +€)。

更新 - 加号运算符实际上是'或'

更新 - €是空字符串

更新 - 字符串长度没有要求。长度为5的字符串将包含2个长度为4的子字符串,前4个字符和最后4个字符

3 个答案:

答案 0 :(得分:0)

我认为教授正在寻找这样的认识,即为了使条件成立,双方都不会被一个零包围。

(0?11)*

为了完成图片,我们还需要包含 n = 1的情况,其中允许0或1(我推测)。

^[01]$|(0?11)*

我在这里使用传统正则表达式,其中[...]表示字符类,|表示“或”,括号表示分组,*指定零重复或更多重复(Kleene star) )。

答案 1 :(得分:0)

编辑:考虑以下正则表达式,其中引用空字符串,并假设字母表仅包含 {0,1,€}

€+(0111)* +(1110)* +(1101)* +(1011)*

答案 2 :(得分:0)

Python 2

import re

# regular expression
# '^' in the start of the expression means from the very start of the string
# "[^1.]*" means match any character unless it is '1'
# '$' in the end of the expression means till the very end of the string
# Summary:
# from the start of the string till the end of it check if we have
# '1' exactly 3 times, 
# and before or after them you might find any type of characters
# and these characters must not be equal '1' ...
expression = '^[^1.]*1[^1.]*1[^1.]*1[^1.]*$'

# testing strings
tests = ["01110", "11100", "00111", "010101", "00100100111", "00100"]


prog = re.compile(expression)

for test in tests:
    print 'matched' if prog.match(test) else 'not matched'