Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Algorithm: Intersection of two regexps

Tags:

c#

algorithm

I'd like to figure out (in runtime) whether or not two regular expressions intersect (i.e. if they do there exist one or more string the matches both regular expressions).

Algorithm needs to be pretty fast as I need to loop through a database and check existing values.

Found some theory on this but no implementations?

like image 520
Niels Bosma Avatar asked Sep 12 '26 10:09

Niels Bosma


2 Answers

The obvious solution would be to convert the regexes to DFAs, compute the intersection of the DFAs (trivial) and see if there's anything the resulting DFA can accept (also trivial). The only hard part is converting the regexes to DFAs, which requires some work.

like image 109
Chris Dodd Avatar answered Sep 14 '26 23:09

Chris Dodd


Here's an implementation in Haskell using partial derivatives of regexps. A comment to that post points out an issue with the approach in Chris Dodd's answer.

like image 25
Darius Bacon Avatar answered Sep 15 '26 01:09

Darius Bacon