Suppose I have a regular expression set R, how can I find a string s that matches as many regular expressions as possible?
For example, if R = {a\*b, b\*, c}
, then s
could be "b"
. I am not sure, maybe z3 solver could be helpful?
Suppose I have a regular expression set R, how can I find a string s that matches as many regular expressions as possible?
For example, if R = {a\*b, b\*, c}
, then s
could be "b"
. I am not sure, maybe z3 solver could be helpful?
Yes, z3 can handle this via regular-expressions and optimization. You can do this directly using SMTLib, or bindings from other languages to z3 (C, C++, Java, Haskell etc.) Below, find Python and Haskell versions:
Python
Using the Python bindings to z3:
When I run this, I get:
which finds the string
b
as you described.Haskell
Here's the same example, coded using the SBV library in Haskell:
When run, this produces:
which also shows you how many of the reg-exps matched. (You can program so it also tells you exactly which ones matched as well.)