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

Monks, The following code fails in the Search and replace task.
my $string ='<vertical name="Businesses" combination="and"> <Busines name="Content"> <description> Replicant consists of Icon, Title1, Title2 +, Description, MOre Title, and More Hyperlink (minimum=0, maximum=10) + </description> <BusinessNames min-entity="1" max-entity="10">'; my $strRepl ='<Bnames name="Businesses" min-entity="1" max-entity="10" +>'; $string =~ m/<BusinessNames (.*)?>/; my $str2Brepl = $`; $string =~ s/$str2Brepl/$strRepl/mi; print "$string";

The $string contents remain unchanged.

Please suggest why and a solution too.

thanks

Replies are listed 'Best First'.
Re: Debug and workaround help
by davido (Cardinal) on Jun 28, 2005 at 06:10 UTC

    You're not checking for success in your first match, so it could be silently failing. Also, definately consider using quotemeta on $str2Brepl.


    Dave

Re: Debug and workaround help
by prasadbabu (Prior) on Jun 28, 2005 at 06:10 UTC

    Add \Q...\E in the search pattern. Now you will get the correct output.

    $string =~ s/\Q$str2Brepl\E/$strRepl/mi; print "$string";

    Prasad

      The Functionality works if the $string value is changed to as below
      my $string ='<item name="a"><text></text></item><vertical name="Busine +sses" combination="and"> <Busines name="Content"> <BusinessNames min-entity="1" max-entity="10">';

      I think the "</description>" needs to be handled differently in the search and replace regex.

        After removing the description part, you are getting correct output, because it has special characters which has to be escaped. Whenever if you have any special characters in the search pattern you need to use \Q...\E. You go through perlre.

        Prasad

Re: Debug and workaround help
by jbrugger (Parson) on Jun 28, 2005 at 06:12 UTC
    Since you placed
    my $str2Brepl = $`;
    $str2Brepl becomes the same as $string.
    perhaps you mean
    my $str2Brepl = $1;
    since you placed brackets around the matching regexp?

    "We all agree on the necessity of compromise. We just can't agree on when it's necessary to compromise." - Larry Wall.
Re: Debug and workaround help
by Animator (Hermit) on Jun 28, 2005 at 10:14 UTC

    Can you please explain what output you want?

    My guess is that you want to replace <Buisnessnames ...> with <Bnames ...> in which case $' is incorrect.

    You could consider using $&, but that IMHO is yet another idea.. Why don't you do one big replace? Why care about extracting the thing via a match and then using it in a subtitution?

    What would be wrong with: s/<BusinessNames (.*?)>/$strRepl/mi; ?

    Also note that you don't need the quotes in print "$string"; I would even suggest dropping them since it makes it a little bit less readable (or atleast to me)

Re: Debug and workaround help
by siliconGopher (Initiate) on Jun 28, 2005 at 06:54 UTC
    When you try and match for the pattern, you are checking for zero or at least one occurance of any alphanum, so even if there is nothing after "<BusinessNames" the match still would not fail. Second even if you tried to print out $str2Brepl the regex that you wanted to catch inside the (.*)would not be reflected as expected. You need to be assigning $str2Brepl as equal to $1 ... $str2Brepl = $1; gopher