roho has asked for the wisdom of the Perl Monks concerning the following question:
It's late, I'm tired, and I must be missing the obvious. I need another pair of eyes to see what I've overlooked. The code sample below tests a file name against a regex containing the same file name. The first test uses forward slashes (and works as expected). The second test uses backslashes and does not work.
Please note: I have tried escaping the backslashes, but it still fails. I have tried running the regex through the "qr()" function, but it still fails. What am I missing here? TIA for any help you can provide.
#!/usr/bin/perl use strict; use warnings; # WORKS my $file = 'C:/tmp'; my $regex = 'C:/tmp'; if ($file =~ m/$regex/i) { print "\nTest with forward slashes -- If stmt is true...\n"; } # FAILS $file = 'C:\tmp'; $regex = 'C:\tmp'; if ($file =~ m/$regex/i) { print "\nTest with backslashes -- If stmt is true...\n"; }
"It's not how hard you work, it's how much you get done."
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Regex Mystery
by tybalt89 (Monsignor) on Apr 13, 2025 at 04:20 UTC | |
by roho (Bishop) on Apr 13, 2025 at 04:44 UTC | |
by harangzsolt33 (Deacon) on Apr 13, 2025 at 15:47 UTC | |
by Anonymous Monk on Apr 13, 2025 at 16:54 UTC | |
|
Re: Regex Mystery
by InfiniteSilence (Curate) on Apr 13, 2025 at 04:26 UTC | |
by choroba (Cardinal) on Apr 13, 2025 at 17:54 UTC | |
by LanX (Saint) on Apr 13, 2025 at 19:15 UTC | |
by roho (Bishop) on Apr 14, 2025 at 04:54 UTC | |
by cavac (Prior) on Apr 18, 2025 at 13:39 UTC | |
by roho (Bishop) on Apr 13, 2025 at 17:14 UTC | |
by cavac (Prior) on Apr 16, 2025 at 11:04 UTC |