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

At the risk of being flamed back to initiate level I ask this: Given a Regexp reference, is there a safe (or known) way to get the underlying regexp? For example, given this code:

my $x = qr/^foo\.c$/; print "$x\n";

I get this result:

(?-xism:^foo\.c$)

which would suggest just extracting what follows the (?-xism:, preceding the final paren would do it. Seems like very cheesy coding though.

The reason for this is an interface that accepts a list of arguments as file matching arguments. If the argument is a scalar, it's assumed to be a wildcard pattern which is converted to a regexp. If it's a Regexp reference it's obviously a regexp. Right now the interface simply converts all to a list of regexp's then loops through that list at match time. I was contemplating converting the list to a | alternation regexp for a single match, but I'd need to "undo" the Regexp references to do that.

Replies are listed 'Best First'.
Re: Getting regexp from Regexp references
by integral (Hermit) on Jan 28, 2003 at 20:09 UTC
    (?-xism:^foo\.c$)
    That is a perfectly valid regexp. The (?- allows one to specifiy regexp option inside the regexp pattern itself.

    In addition to build your alternation there is no need to extract the original regexp, since you can quite safely interpolate Regexp objects into patterns:

    my (@a) = (qr/^foo\.c$/, qr/^bar\.c$/, ); /$a[0]|$a[1]/; #or my $re = join("|", @a); /$re/;

    --
    integral, resident of freenode's #perl
    
      (?-xism:^foo\.c$)
      That is a perfectly valid regexp. The (?- allows one to specifiy regexp option inside the regexp pattern itself.
      Actually, it's "(?OPT-NOPT:PAT)", including the colon, which is a variation on the old familiar theme of "(?:PAT)". The hyphen and following negated options are optional.

      I'll give a somewhat more detailed example:

      qr/^foo.*\.c$/si
      as a string is
      (?si-xm:^foo.*\.c$)
      Thus: the enabled options come right after the question mark, disabled options come after that, between a hyphen and the colon.

      These are the only four options that can actually be used in this manner. Thus: the list of options is complete.

      Sometimes the obvious escapes me. Thanks. 8-)

Re: Getting regexp from Regexp references
by Gilimanjaro (Hermit) on Jan 28, 2003 at 20:34 UTC
    What you see is what you get... :)

    You can actually use the string you're getting as a regex... The (?imsx-imsx:...) syntax can be used within a // construct to enable/disable the modifiers that are normaly placed outside the //'s.

    This syntax is actually encouraged in the perlre manpage for usage with dynamic patterns, which is exactly what you seem to be dealing with.

    And the 'compiled' regexes that qr returns can be used in another regex, so you should be able to just plain join them with '|', and qr the result...