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."