Finding the intersection between two regexes

Mark Fowler mark at twoshortplanks.com
Mon Apr 21 03:14:48 BST 2014


On Sunday, April 20, 2014, David Cantrell <david at cantrell.org.uk> wrote:

> Can anyone point me at some code on the CPAN that, given two regexes,
> can figure out whether there are any bits of text that will be matched
> by both?


I'm not sure I understand the question here, or moreover why you want to do
this..is it just an intellectual exercise?

If it's just a matter of wanting a single Perl regular expression that can
match something iff both of these other regular expressions would match,
surely you can just do this by inserting the second regular expression at
the beginning of the first encapsulated in a zero-width positive look ahead
assertion (with suitable variable length doodads to pad if they're not
anchoring at the same place in the string.)

What the link is talking about seems to be converting a regular expression
down into a finate state machine and then combining that finate state
machine with another finate state machine (I.e. non deterministic, being
turned back into deterministic with maths). I can see how that's possible
for a strict regular expression, but as you say, not for a true Perl
non-regular regular expression.

So...why do you want to do this?

Mark


More information about the london.pm mailing list