roho has asked for the wisdom of the Perl Monks concerning the following question:

Greetings monks,

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
    if ($file =~ m/\Q$regex/i) {

    because \t is a tab.

      Thanks tybalt89. I knew it must be something obvious. Time to rest now.

      "It's not how hard you work, it's how much you get done."

      Wait, wouldn't this be more appropriate?:

            if ($file =~ m/\Q$regex\E/i) ...

        theyre exactly the same
Re: Regex Mystery
by InfiniteSilence (Curate) on Apr 13, 2025 at 04:26 UTC
    ... # FAILS $file = 'C:\tmp'; $regex = qr~C:\\tmp~; if ($file =~ m/$regex/i) { print "\nTest with backslashes -- If stmt is true...\n"; }

    Prints...

    Test with forward slashes -- If stmt is true... Test with backslashes -- If stmt is true...

    FYI coding while tired is a recipe for adding errors to code that you will find hard to find later.

    Celebrate Intellectual Diversity

      > FYI coding while tired is a recipe for adding errors to code that you will find hard to find later.

      But sometimes, coding while tired delivers at least something before the deadline. Life is hard.

      map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
        There are various benefits of coding before sleep
        • you get started
        • you keep your sleeping brain busy with non social stuff
        • you may wake up with a solution
        • you may not contribute to the gene pool of the human race

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        see Wikisyntax for the Monastery

        But sometimes, coding while tired delivers at least something before the deadline.

        Yes, it does. Mostly the privilege of fielding support calls all weekend long, therefore pissing off the significant other. And the option to watch your favourite movies all night long, since you're gonna sleep on the couch anyway...

        PerlMonks XP is useless? Not anymore: XPD - Do more with your PerlMonks XP
        Also check out my sisters artwork and my weekly webcomics
      Thanks InfiniteSilence. You're right, working when tired produces diminishing returns.

      "It's not how hard you work, it's how much you get done."

        In my case, that would be negative returns.

        When i'm tired, i'm basically more productive doing nothing than when i'm touching code.

        Also, for a few hours after i'm forced by my body to take my pain meds for bad back (usually once a month on average). And being on the autistic spectrum, there are sometimes days my brain just refuses to do anything productive and i just can't concentrate on one singel thing for more than a minute or so.