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

In the string if there is no closing ")" and has "(" then, how to escape ')' in the string while substitution.
fg-(we.xml
#!/usr/bin/perl while($line=<DATA>){ $Name_pre=$line; $Name=$line; $Name=~ s/&amp;amp;/-/g; $Name=~ s/&amp;/-/g; $Name =~ s/-$//g; $Name_pre=~ s/$Name_pre/$Name/g; print "The name is $Name_pre\n"; } __DATA__ fg-&amp;amp;&amp;+(and(we-
The substitution is not performed on the above code.Can you please tell me how to resolve this

Replies are listed 'Best First'.
Re: Substitution for braces
by Corion (Patriarch) on Jul 08, 2009 at 07:17 UTC
      Can I use the code similar to this?
      #!/usr/bin/perl while ($line = <DATA>){ $line =~ s/<cc>([\s\S]+?)<\/cc>/defined ($1) ? "(&amp;amp;|&amp;)" : ' +-'/eg; print $line; }
      This prints out (&amp;|&). But replacing of &amp; and & doesnot gets replaced with '-'.

        Is this a different question to your original question? I don't understand it. The data in your original question does not have <cc> in it.

Re: Substitution for braces
by grizzley (Chaplain) on Jul 08, 2009 at 08:36 UTC
    $Name =~ s/-$//g;

    //g switch in above regexp doesn't make sense, can be omitted.

    $Name_pre =~ s/$Name_pre/$Name/g;

    I have been thinking intensively for a moment, but cannot find any example, where it would be better than

    $Name_pre = $Name;

    What exactly is this code supposed to do?

      grizzley I couldn't agree more...

      Corion fixes the original Unmatched ( in regex; marked by <-- HERE in m/fg-&amp;amp;&amp;+(and( <-- HERE we- error,
      but beyond that i am struggling...

      #!/usr/bin/perl use warnings; use strict; while(my $line=<DATA>){ my $name_pre=$line; my$name=$line; print "1: name $name * name_pre $name_pre \n"; $name=~ s/&amp;amp;/-/g; print "2: name $name * name_pre $name_pre \n"; $name=~ s/&amp;/-/g; print "3: name $name * name_pre $name_pre \n"; $name =~ s/-$//; print "4: name $name * name_pre $name_pre \n"; $name_pre=~ s/\Q$name_pre\E/\Q$name\E/g; print "5: name $name * name_pre $name_pre \n"; $name_pre =~ s/\\//g; print "6: name $name * name_pre $name_pre \n"; } __DATA__ fg-&amp;amp;&amp;+(and(we-

      Prints:

      1: name fg-&amp;amp;&amp;+(and(we- * name_pre fg-&amp;amp;&amp;+(and(we- 2: name fg--&amp;+(and(we- * name_pre fg-&amp;amp;&amp;+(and(we- 3: name fg---+(and(we- * name_pre fg-&amp;amp;&amp;+(and(we- 4: name fg---+(and(we * name_pre fg-&amp;amp;&amp;+(and(we- 5: name fg---+(and(we * name_pre fg\-\-\-\+\(and\(we\ 6: name fg---+(and(we * name_pre fg---+(and(we

      Just a something something...
      The actual program code is below:
      Open FH,’file.txt’; while (<$FH>) { $arr[$i].="$_"; } close ($FH); $text=$arr[$i]; $text =~ /<cc>([\s\S]+?)<\/cc>/; $Name_pre=$1; $Name=$1; $Name=~ s/&amp;amp;/-/g; $Name=~ s/&amp;/-/g; $Name =~ s/-$//g; $st=$Name; $char=chop($st); if($char eq '-') {$Name=~ s/$//g;} $arr[$i]=~ s/$Name_pre/$Name/g;
      Is it the right way of coding. When I execute the program with the $Name_pre as
      fg-&amp;amp;&amp;+(and(we-
      the program terminates giving an error at the line
      $arr[$i]=~ s/$Name_pre/$Name/g;
      as
      Unmatched ( in regex; marked by <-- HERE in m/fg-&amp;amp;&amp;+(and( +<-- HERE we- /

        There are many other problems in the script as presented, but you immediate problem can probably be solved by changing the offending line to:

        $arr[$i]=~ s/\Q$Name_pre\E/$Name/g;

        True laziness is hard work
        Take a look at perlre for more info and the opportunity to learn, but basically you'll either have to escape them using backslashes or use \Q \E to contain your $Name_pre string, though as pointed out above, why are you assigning the value in such an odd way?

        you could substitute all ( in a string with the regex $string =~ s/\(/$cheesesauce/g;

        Oh Goddess, look this code needs a lot of work, take a look at the following

        open($FH,'<','file.txt'); $arr[$i]=join('',(<$FH>)); close ($FH); $arr[i] =~ /<cc>([\s\S]+?)<\/cc>/$1/; $arr[i] =~ s/&amp;amp;/-/g; $arr[i] =~ s/&amp;/-/g; $arr[i] =~ s/\(/-/g; # I assume $arr[i] =~ s/-$//; if (substr($arr[i],-1,1) eq '-') { $arr[i]=~ s/-$//g; # I assume you meant, though you've already done + it above }
        I'm sure it's far from perfect, but an improvement on your current code nonetheless