Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

regex error

by anaconda_wly (Scribe)
on Apr 06, 2013 at 10:12 UTC ( [id://1027274]=perlquestion: print w/replies, xml ) Need Help??

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

I got error in regex: Sequence \k... not terminated in regex; marked by <-- HERE in m/"c:\qa\k <-- HERE onductor\bin"/ at...
$binpath="c:\\qa\\kontor\\bin"; if( $ENV{PATH} !~ /$binpath/i) # error here
It's OK when I type directly not using a string var.
if( $ENV{PATH} !~ /c:\\qa\\kontor\\bin/i)
Anyone can point out my error?Thanks

Replies are listed 'Best First'.
Re: regex error
by aitap (Curate) on Apr 06, 2013 at 12:25 UTC
Re: regex error (backslashing)
by LanX (Saint) on Apr 06, 2013 at 15:30 UTC
    > It's OK when I type directly not using a string var.

    Different things!

    > $binpath="c:\\qa\\kontor\\bin"

    here you are backslashing to avoid string-interpolation within "doublequotes"

    > if( $ENV{PATH} !~ /c:\\qa\\kontor\\bin/i)

    here you are backslashing to avoid special escape \commands like \k

    So in your code you need 2 levels of escapes!

    $binpath="c:\\\\qa\\\\kontor\\\\bin";

    Do better combine 'singlequotes' and quotemeta \Q and you avoid any explicit backslashing.

    Cheers Rolf

    ( addicted to the Perl Programming Language)

      Thanks to all. I use index function also solve this issue.
Re: regex error
by Anonymous Monk on Apr 06, 2013 at 10:54 UTC
Re: regex error
by igelkott (Priest) on Apr 06, 2013 at 15:11 UTC

    As other have pointed out, you should generally use the correct type of quoting.

    But, if you're just using hard-coded paths, you can skip the fancy quotes if you switch to unix-style paths. In other words, change the assignment to $binpath="c:/qa/kontor/bin";. As mentioned in this very old post, paths are converted automatically.

    Personally, I prefer this method so I can use the same script on PC and Linux. Well, at least for relative directories.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-18 18:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found