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

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?

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$

Need Your Help

JPA: Eclipselink not respecting em.remove()

jpa glassfish ejb eclipselink

I need to delete one entity and create another:

R: Flagging Sample from Same Specimen w/ Different DOB

r date duplicates dob data-cleaning

I have a dataset that has duplicate samples that have a different date of birth. This obviously should not be the case, so I am trying to come up with a way to flag/mark those particular samples. ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.