Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Storing regexen in a DB and using them

by Anonymous Monk
on Jan 27, 2014 at 15:27 UTC ( [id://1072237]=perlquestion: print w/replies, xml ) Need Help??

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

Dear monks,

say for example I would like to store regular expressions in a database in a way I would use it in a web form to check for valid user input for a form element.

Let's say I have my expression stored in a varchar column like this: /[a-z]/i

When I now retrieve that from the database and have the expression stored in a scalar $regex - How would I be able to use it in my code to match it against some $text?

Replies are listed 'Best First'.
Re: Storing regexen in a DB and using them
by kennethk (Abbot) on Jan 27, 2014 at 15:33 UTC
    You can store a reference to a regular expression using qr// (see Regexp Quote Like Operators). The stringified version of that will store easily in a database, and can be bound like any other regular expression with $text =~ $regex (see Binding Operators) or interpolated into other expressions, a la m/^$regex$/.

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Re: Storing regexen in a DB and using them
by davido (Cardinal) on Jan 27, 2014 at 17:29 UTC

    kennethk is right; store a stringified pattern constructed with the qr// operator. But there's a distinction to be made between storing a reference to a Regexp object, and storing a stringified Regexp object. Consider the following code:

    my $regexp = qr/PATTERN/; print {$FH} $regexp; # writes (?^u:PATTERN) to file.

    The Regexp object returned by the qr// operator has stringification overloaded so that you never see something like Regexp(0xabcdef). This is very convenient, as it means that Regexp objects are easily interpolated into larger patterns, larger Regexp objects, or into strings as larger patterns are assembled.

    But there is one nuance here that might be missed: A Regexp object is a compiled regular expression. Stringifying a regexp object returns a string representation of a version of the original pattern that is semantically identical (if not syntactically) to the original pattern. But it is only a string, and before it's useful to Perl on an internals level, it will need to be compiled again. This happens quietly behind the scenes. But it does carry with it a (minor) runtime cost.

    Nevertheless, it is the best approach. Just be aware that you're storing a string returned by implicit stringification of a Regexp object; you're not storing the object itself, nor a reference to it.


    Dave

Re: Storing regexen in a DB and using them
by TomDLux (Vicar) on Jan 27, 2014 at 15:32 UTC

    You mean something like ...

    if ( $text =~ m{$regex} ) { .... }

    As Occam said: Entia non sunt multiplicanda praeter necessitatem.

Re: Storing regexen in a DB and using them
by FloydATC (Deacon) on Jan 28, 2014 at 11:42 UTC

    No special action is required, the following works as expected as long as $regex contains a valid regex:

    if ($input =~ /$regex/) { ... }
    -- FloydATC

    Time flies when you don't know what you're doing

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1072237]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-19 23:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found