Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re^2: creating qr from existing regex

by Bobert1234 (Novice)
on Feb 08, 2018 at 18:26 UTC ( #1208736=note: print w/replies, xml ) Need Help??

in reply to Re: creating qr from existing regex
in thread creating qr from existing regex

i figured it out. The issue was that when i created the $re variable, it was adding the slashes. by using just what was inside the slashes of the original string, i managed to get it to work
if(index($_[0],"qr/") >= 0){ $_[0] =~s/qr[\/](.*?)([^\\])[\/](i?)$/$1$2/s; print $fh Dumper($_[0]); if($3=='i'){ $_[0] = qr/$_[0]/i; }else{ $_[0] = qr/$_[0]/; } print $fh Dumper($_[0]); }

Replies are listed 'Best First'.
Re^3: creating qr from existing regex
by poj (Monsignor) on Feb 08, 2018 at 19:08 UTC

    Maybe use split

    my $string = '/.*uba$/i'; my @f = split '/',$string; my $qr = ($f[-1] eq 'i') ? qr/$f[1]/i : qr/$f[1]/; my $cursor = $collection->query( { name=> $qr });

      that could work in general, but the input is dynamic, and someone might put in an escaped slash, i.e. \/

        c:\@Work\Perl\monks>perl -wMstrict -le "my $string = '/.*u\/ba$/i'; ;; my $rx = eval 'qr' . $string; print $rx; print 'A: match' if 'u/Ba' =~ $rx; ;; my $ry = qr{ \A foo $rx }xms; print $ry; print 'B: match' if 'foolubatU/bA' =~ $ry; " (?i-xsm:.*u/ba$) A: match (?msx-i: \A foo (?i-xsm:.*u/ba$) ) B: match

        Update: Of course, given that you're dealing with user-supplied data and considering the danger of an injection attack, maybe one should think twice about an eval-based approach.

        Give a man a fish:  <%-{-{-{-<

        In addition to what AnomalousMonk said. Plain regular expressions can be constructed to be DoS attacks; even unintentionally. The handling varies from Perl version to version and I am pretty sure regular expressions were impossible to interrupt with a timeout historically so the "attack" was impossible to guard against. I have no idea if this is still the case in newer versions or when it changed if it did.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1208736]
and the radiator hisses contentedly...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (7)
As of 2018-02-21 11:57 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (279 votes). Check out past polls.