Defining constraints in Z3 using Boolean operators

为君一笑 提交于 2019-12-02 03:33:25

The best way to go about this would be to simply use z3's regular-expression matching facilities:

from z3 import *
import string

lower  = Union([Re(c) for c in string.lowercase])
upper  = Union([Re(c) for c in string.uppercase])
digs   = Union([Re(c) for c in string.digits])
uscore = Re('_')

charset = Union(lower, upper, digs, uscore)
lang    = Plus(charset)


s = Solver()
test = String("test")
s.add(Length(test) == 10)
s.add(InRe(test, lang))

print s.check()
print s.model()

This prints:

sat
[test = "9L25ZPC1B8"]

Or you can use it to test whether particular strings belong to the regular-expression you've defined:

>>> print (simplify(InRe("hEllO_123", lang)))
True
>>> print (simplify(InRe("%$", lang)))
False
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!