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

I want to build a regular expression by combining several "user-specified" regular expressions. Then, I'll apply the master regex by saying
m/$regex/x.  
Now the problem is, the user may have originally specified the regex as m:a/b[c/d]:, or they may use the m//x flag on the expression.

1) The question is, does an module already exist that will translate a regex for use with a standard delimeter?
2) If not, what are the gotchas that have to be addressed in writing such code. I have identified these:
   a) deal with basic \/ constructs.
   b) avoid separators in a set [/]
   c) deal with the /x option

Replies are listed 'Best First'.
Re: Converting RegExs.
by merlyn (Sage) on Sep 02, 2000 at 21:42 UTC
    Perhaps you are worrying about something that isn't a problem. Witness:
    my $pat = "/etc/"; my $string = "/usr/etc/config"; if ($string =~ m/$pat/x) { print "it matches\n"; }
    This works just fine. The included variable contents in $pat cannot cause the delimiters to be prematurely recognized; that phase is already long past.

    -- Randal L. Schwartz, Perl hacker

      Oh. Silly me. DWIM is such a good concept. Thanks.
      Hmm,

      What if the user entered m/foo/x when asked for a regex? Technically, the regex the user wanted was foo, but they entered the whole match command instead:

      my $keywords = "NSA|cocaine|Lybia|assasin(at(ion|e))?|CIA"; print "Enter regex matching other keywords:"; my $userwords = <STDIN>; # user enters "m/MI-6/x" if ($string =~ m/$keywords|$userwords/x) { print "Found a terrorist! $string\n"; }
      Obviously, m/MI-6/x (they want to match "MI-6", not "m/MI-6/x"). How could that be corrected for?
        Obviously, m/MI-6/x (they want to match "MI-6", not "m/MI-6/x"). How could that be corrected for?
        By first answering the question: "how will you know they entered a piece of Perl code instead of simply the regular expression?"

        Most of programming is asking yourself how you know something. The code to teach the machine to know it falls out from that.

        So, how would you know whether they entered a piece of Perl code instead of a regex? If you decide arbitrarily that it's when something begins "m slash", then there's your solution: use a regex to extract the piece. Maybe they select another form element that says "I'm entering a regex instead of a piece of Perl code", or something. But an arbitrary string is neither a regex nor a piece of Perl code (maybe they wanted to match something that begins with m slash).

        Of course, the simplest way to correct for it is with humanware instead of DWIM-ware. Put a big message by the input box that says "enter a regex, not a piece of Perl code!". Sometimes, the simplest solutions are the best.

        -- Randal L. Schwartz, Perl hacker

RE (tilly): Converting RegExs.
by tilly (Archbishop) on Sep 02, 2000 at 21:45 UTC
    If you really want them to use REs and not just search for fixed strings, look at the qr function in perlop. To get your local documation on it try perldoc perlop, type "/qr" (then return), then "n" until you get to the right section.