I need to go back from the stringified form of a regexp to a real regexp. The regexp is stringified when I use it as the key to a hash (which I have to do as the current API dictates the use of a hash here).

The little bit of code below explains what happens and what I have done to get the regexp back.

#!/usr/bin/perl -w use strict; my $regexp = qr/^x/i; # a regexp # stringify it my $hash = { $regexp => 1 }; # keys are stringifi +ed my $string = (keys %$hash)[0]; # a string print "$string (ref: ", ref $string,")\n"; # and I prove it! # the string is: '(? +i-xsm:^x)' # regex-ify it if( $string=~m{^\(\?([xism]*)-([xism]*):(.*)\)$}) # match the interest +ing bits { my $nregexp= qr/(?$1:$3)/; # rebuild it print "$nregexp (ref: ", ref $nregexp,")\n"; # a regexp again! # it stringifies as +'(?-xism:(?i:^x))' }

My questions are: did I forget anything? Does it work accross versions of perl (I tested it under 5.8.6)? Is there anything simple that I forgot/did not know about?


In reply to Converting a stringified regexp back into a regexp by mirod

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.