Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Regex Search String in a Variable

by Discipulus (Canon)
on May 01, 2021 at 17:07 UTC ( [id://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

L*

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 (Bishop) 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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2024-04-25 15:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found