roho has asked for the wisdom of the Perl Monks concerning the following question:
When I put the search string directly in the regular expression, it works (the text string is modified).
When I put the search string in a variable, it does not work (the text string is not modified).
Note: I tried running the search string variable through the "qr" function, but that did not help.
I feel like I am missing something very basic here, but I just can't see it. TIA for any suggestions.
#!/usr/bin/perl use strict; use warnings; my ($text, $search); $text = "\\x1\\x2\\x3\\x4"; print "\nUsing literal in regex: (success)\n"; print "Before: text = $text\n"; $text =~ s/x2\\.+\\x4/new/; print " After: text = $text\n"; $text = "\\x1\\x2\\x3\\x4"; $search = "x2\\.+\\x4"; print "\nUsing variable in regex: (failure)\n"; print "Before: text = $text\n"; $text =~ s/$search/new/; print " After: text = $text\n";
"It's not how hard you work, it's how much you get done."
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Regex Search String in a Variable
by LanX (Saint) on May 01, 2021 at 17:46 UTC | |
by johnaj (Sexton) on May 01, 2021 at 20:31 UTC | |
by LanX (Saint) on May 01, 2021 at 20:56 UTC | |
by roho (Bishop) on May 01, 2021 at 21:01 UTC | |
|
Re: Regex Search String in a Variable
by Discipulus (Canon) on May 01, 2021 at 17:07 UTC | |
by roho (Bishop) on May 01, 2021 at 20:59 UTC | |
|
Re: Regex Search String in a Variable
by AnomalousMonk (Archbishop) on May 01, 2021 at 21:24 UTC | |
by LanX (Saint) on May 02, 2021 at 12:07 UTC | |
by AnomalousMonk (Archbishop) on May 03, 2021 at 01:22 UTC | |
by LanX (Saint) on May 03, 2021 at 12:37 UTC |