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

regex error

by anaconda_wly (Scribe)
on Apr 06, 2013 at 10:12 UTC ( #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 (Deacon) on Apr 06, 2013 at 12:25 UTC
Re: regex error (backslashing)
by LanX (Bishop) 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!


    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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1027274]
Approved by Perlbotics
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2017-12-18 02:04 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (466 votes). Check out past polls.