Python正则表达式; 0匹配0或1

log*_*eyg 1 python regex

我有一个较短的字符串,s我试图匹配较长的字符串s1.1的匹配1,但0将匹配0或1.

例如:

s = '11111' would match s1 = '11111'

s = '11010' would match s1 = '11111' or '11011' or '11110' or '11010'
Run Code Online (Sandbox Code Playgroud)

我知道正则表达式会使这更容易,但我很困惑从哪里开始.

Dan*_*ocq 6

替换0with的每个实例[01]以使其匹配01:

s = '11010'
pattern = s.replace('0', '[01]')
regex = re.compile(pattern)

regex.match('11111')
regex.match('11011')
Run Code Online (Sandbox Code Playgroud)