http://qs321.pair.com?node_id=175710

PetaMem has asked for the wisdom of the Perl Monks concerning the following question:

Hello Regexp Alcolytes!

Disclaimer: Don't point me to Math::Roman, read the node. :-)

Probably the title of this node is horribly wrong, so I try to explain my need in more detail. Let's say you have a set of regexps. Now lets say you are testing each of these regexps against a string to see if it matches or not. Something like this Roman Numeral Thingie. Meanwhile it looks slightly better:

if($str =~ s/XLVIII$/IL/ or $str =~ s/VIII$/IX/ or $str =~ s/III$/IV/ or $str =~ s/DCCCXCIX$/CM/ or $str =~ s/CCCXCIX$/CD/ or $str =~ s/LXXXIX$/XC/ or $str =~ s/XXXIX$/XL/ or $str =~ s/(I{1,2})$/$1I/ or $str =~ s/CDXCIX$/D/ or $str =~ s/CMXCIX$/M/ or $str =~ s/XCIX$/C/ or $str =~ s/I([VXLCDM])$/$1/ or $str =~ s/([VXLCDM])$/$1I/) { } return $str;
And now there is a subroutine to answer the question if a given string actually is a roman numeral:
if($str !~ /[IVXLCDM]+/ || $str =~ /([IXC])\1{3,}/ || $str =~ /([VLD])\1{1,}/ || $str =~ /I[VXLCDM][IVXLCDM]/ || $str =~ /V[XLCDM][VXLCDM]/ || $str =~ /L[CDM][LCDM]/ || $str =~ /X[LCDM][XLCDM]/ || $str =~ /C[DM][CDM]/ || $str =~ /DM[DM]/) { return 0; } return 1;
So lets return to the question. Given a set of regexps and given a wanted logical concatenation (lets start with AND,OR) of these regexps, is there any mechanism to create a smaller, more eficient set of regexps that will have the same effect?

I feel remembered to a similar problem in boolean logic, where you apply some de morgan, do transformations on complex boolean formulae and - sometimes - end up with smaller nifty formulae. Thought same should be possible with regexps.

Thoughts? Answers? Meditation?

Bye
 PetaMem