Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Regex Mystery

by roho (Bishop)
on Apr 13, 2025 at 03:47 UTC ( [id://11164679]=perlquestion: print w/replies, xml ) Need Help??

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

      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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11164679]
Approved by marto
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2025-04-17 17:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.