Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re: Regex Search String in a Variable

by Discipulus (Abbot)
on May 01, 2021 at 17:07 UTC ( #11131933=note: print w/replies, xml ) Need Help??

in reply to Regex Search String in a Variable

Hello roho,

> Note: I tried running the search string variable through the "qr" function, but that did not help.

qr helps

#!/usr/bin/perl use strict; use warnings; my ($text, $search); $text = "\\x1\\x2\\x3\\x4"; $search = qr/x2\\.+\\x4/; print "\nUsing variable in regex: (qr)\n"; print "Before: text = $text\n"; $text =~ s/$search/new/; print " After: text = $text\n"; __END__ Using variable in regex: (qr) Before: text = \x1\x2\x3\x4 After: text = \x1\new


There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Replies are listed 'Best First'.
Re^2: Regex Search String in a Variable
by roho (Chancellor) on May 01, 2021 at 20:59 UTC
    Thanks Discipulus. I now see the reason my qr didn't work. It was because I first assigned the search string to the variable, then reassigned the variable (run through qr) to itself, which did not double the backslashes.

    my $search = "x2\\.+\\x4"; $search = qr($search);

    "It's not how hard you work, it's how much you get done."

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11131933]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2021-09-17 05:14 GMT
Find Nodes?
    Voting Booth?

    No recent polls found