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

regex search and replace issue

by Anonymous Monk
on Nov 11, 2011 at 08:05 UTC ( [id://937540]=perlquestion: print w/replies, xml ) Need Help??

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

if (lc($Message) =~ /^db /) { print "Content-type:text/html\n\n"; print "c=$Message<br>\n"; $Message = s/^db //; $Message = s/^Db //; $Message = s/^dB //; $Message = s/^DB //; print "m=$Message"; exit; }
gives result:
c=Db test4<br> m=

I'm expecting the Db(space) to be removed but the whole expression is being taken out!

Replies are listed 'Best First'.
Re: regex search and replace issue
by Anonymous Monk on Nov 11, 2011 at 08:21 UTC

    Assignment operator = is not the same as binding operator =~

    If you want to make matches, or substitutions, on $variable, you need to use binding operator

      oh, I'm as careless as careless can be. Thanks to all for pointing it out and helpful suggestions
        Perl can help prevent you from being careless... if you use strict and warnings. You would have gotten a warning message which would have pointed you to the offending lines.
Re: regex search and replace issue
by MVS (Monk) on Nov 11, 2011 at 08:26 UTC

    Anonymous Monk has the right solution, but just wanted to add that you can simplify your regexes using character classes. You can replace all four with:

    $Message =~ s/^[Dd][Bb] //;
      Or by using the case insensitivity modifier to the regex.
      $Message =~ s/^db //i;

      print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."
Re: regex search and replace issue
by mrstlee (Beadle) on Nov 11, 2011 at 10:05 UTC
    Just to flesh out Utilitarian's answer:
    if ($Message =~ /^db /i) { print "Content-type:text/html\n\n"; print "c=$Message<br>\n"; $Message =~ s/^db //i; print "[m=$Message]\n"; exit; }

      Or, since you're exiting and therefore don't need the modified $Message further down, just capture any trailing stuff and print once:

      if ($Message =~ /db (.*)/i){ print "Content-type:text/html\n\n", "c=$Message<br>\n", "[m=$1]\n"; exit; }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2024-04-26 08:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found