Let's say that we have a regex and an index i
. If we suppose that the set of strings that match our regex are sorted in a lexicographical order, how could we get the i
element of this list?
Edit : I added this simple example for more explanation:
input :
regex="[a-c]{1,2}";
index=4;
In this case the ordered list of strings that this regex matches contains the following elements:
a
aa
ab
ac
b
ba
bb
bc
c
ca
cb
cc
output :
The 4th element, which is ac
ps: It's known that a given regex could match an infinite number of strings. This shouldn't have an impact on the process of extracting the element in the given finite index
.
Best Answer
A regular expression maps directly to a finite state machine. Generating strings from the FSM is straightforward with a backtracking algorithm. The example is for the regular expression /aa?b/