>>>>> "SDH" == Socheavy D Heng <[EMAIL PROTECTED]> writes:

  > I was having some problems with the regular expression in the code down
  > below. Can anybody tell me why the replacement doesn't work. I've used
  > various modifiers, but nothing really helped.

you have several problems and misconceptions here. also your goal is not
clear at all to me.

  > #!/usr/local/mysql/perl
  > use strict;

good.

  > my %hash=();

no need for the () as my hashes will be empty.

  > # original string to be replaced
  > $hash{string} = "(617)
  > 786-8999";

i assume you wanted that embedded newline in there.

  > $hash{replacement} = $hash{string};

  > foreach my $key (keys %hash) {
  >   $hash{$key} =~ s/\(/\\\(/igm;
  >   $hash{$key} =~ s/\)/\\\)/igm;
  >   $hash{$key} =~ s/\-/\\\-/igm;

the /i and /m modifiers are useless there. /i involves case and you have
no alpha chars in your regexes. /m affects anchors (^ ad $) and you have
neither in your regexes. and you can combine all three into one simpler
line of code:

        $hash{$key} =~ s/([()-])/\\$1/g;

  > }

  > $hash{replacement} =~ s/\n//igm;
  > $hash{replacement} =~ s/\r//igm;
  > $hash{replacement} =~ s/\t//igm;

same here. combine them all into one regex. or even better use tr:

        $hash{replacement} =~ tr/\t\r\n//d ;

  > # print out strings
  > print "STRING: $hash{string}\n";
  > print "REPLACEMENT: $hash{replacement}\n";

  > $hash{string} =~ s/$hash{string}/$hash{replacement}/igm; # do the
  > replacement

the problem you have here is that the \ char you added is now viewed as
data and not an escape char for the next char. you escaped them too
early. the proper way to escape for regexes is with \Q

        $hash{string} =~ s/\Q$hash{string}/$hash{replacement}/;

that works and you need no modifiers

let perl do the work for you. you are working too hard. :)

uri

-- 
Uri Guttman  ------  [EMAIL PROTECTED]  -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org
_______________________________________________
Boston-pm mailing list
[EMAIL PROTECTED]
http://mail.pm.org/mailman/listinfo/boston-pm

Reply via email to