Suppose we have some text and a regular expression that matches it. Question: if I apply the same expression to text backwards (starting from the last letter to the first one), will it still match?
regex —–> text
xereg –?–> txet
In practice that seems to work, the question is rather about what the theory says about the general case.
Not if you use the Kleene star – if you reverse the regex, you will end up with an invalid regex or one that matches a different pattern:
ab*->*ba(invalid syntax)a*b->b*a(the first one matchesaaabbut notabbb, while the second one matchesbbbabut notbaaa)On the other hand, I’m quite sure that it would be possible to design an algorithm that, given a regex, produces a regex that matches the reverse strings. The following recursive algorithm should work (if r is a regex, rev(r) means the regex that matches the reversed strings):