Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

unable to differentiate last word and intermediate word in a sentence

by suniln
on Sep 06, 2010 at 05:40 UTC ( #858989=perlquestion: print w/ replies, xml ) Need Help??
suniln has asked for the wisdom of the Perl Monks concerning the following question:

Hi All, I I have following piece of code to fetch intermediate and last node of a string. for e.g the input lines are as follows. C:\abc\dfg C:\abc\dfg\#@$#@$@ $key="C:\abc\dfg"; if( $line =~ /$key\\\S+\\/){ # if this is not end node ($pat)=$line=~/$key\\(\S+)\\/; }else{ # if end node ($last_node)=$line=~/$key\\(.*)/; } What is wrong in this reg exp... Thanks, Sunil

Comment on unable to differentiate last word and intermediate word in a sentence
Re: how insert back slash to a string that has only special characters or mixture of both
by suhailck (Friar) on Sep 06, 2010 at 06:10 UTC
    perl -le '$_=q{hi! how are you, man.};s/([!@#$%^&(),.}{[\]+-])/\\$1/g; +print' hi\! how are you\, man\.

      The quotemeta function do this.

      $var="!@#$%^&(),.}{[]+-"; print "\n$var"; $var_with_slash=quotemeta($var); print"\n$var_with_slash";
      suhailck:

      That may not be (see "Update") what OP sought: "for each special character in..." a string of special chars (only) .

      Emphasis supplied.

      Update Oops. Apologies to suhailck!

      I missed the fact that the title explicitly allows for a "mixture" (and relied on OP's narrative and example). Hence, suhailck's reply is arguably better than the next, since the second, among other things, will convert #$%^ to #0\^ and escape the spaces, if a string of "words" is inserted in the $var

      my $var="!@#$%^&(),.Now is the time}{[]+-"; print "\n$var"; my $var_with_slash=quotemeta($var); print"\n$var_with_slash";

      outputs:

      !@#0^&(),.Now is the time}{[]+- \!\@\#0\^\&\(\)\,\.Now\ is\ the\ time\}\{\[\]\+\-

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (8)
As of 2014-12-26 20:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (176 votes), past polls