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."
In reply to Regex Search String in a Variable by roho
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |