Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

perl: how to get the original regex from the precompiled version?

Tags:

perl

Simple code:

use 5.014;
use warnings;

my $re = <DATA>;
chomp $re;
my $re2 = qr/$re/;
say $re2;
__END__
^\w$

result:

(?^u:^\w$)      #added the (?^u:

Is any correct way to decompile $re2 getting back the original regex?

Motivation: the regex is an config value, so need:

  • read it
  • compile it
  • save it to the file for the later use.

But can't save the compiled regex for the later use, because in every compiling the regex got expanded with the (?^u:, so after several cycles i ended with like:

(?^u:(?^u:(?^u:(?^u:(?^u:^\w$)))))

therefore the question are:

  • is here any correct way, how to save the compiled version?
  • if no way - how to decompile, to getting the original version?
  • any idea?
like image 628
jm666 Avatar asked Nov 30 '11 18:11

jm666


1 Answers

While I would just keep the string copy around for data usage, and then compile a copy when I needed to use it, you can also use the regexp_pattern function from the core re module to return the pattern used to create a compiled regex:

use re 'regexp_pattern';

print regexp_pattern qr/^\w$/;

prints

^\w$
like image 110
Eric Strom Avatar answered Sep 19 '22 05:09

Eric Strom