Hello,
(snip)
I am trying to use the s/// operator to remove it, by doing this:
 
while(<INPUT>)
   {
     $_ =~ s/\[*\]//;
     $_ =~ s/\(*\)//;
     print $_;
   }
(snip)
 
The method used is incorrect. 
$_ =~ s/\[*\]//;     ---> This says that the search is for opening
parenthesis (zero or more occurrences of it, since a '*' follows '[')
followed by a close parenthesis.  
 
(snip)
so if the input is:
*MOT:   I'm gonna first [//] first I wanna use em all up .
(snip)
 
In this input, considering ur search pattern, close parenthesis ']' is
found. Before that, the character is '/which is not '[' (zero occurrence
of it). Thus, it's a valid search. So only ']' is removed.
 
The correct search pattern ought to be: 
$_ =~ s/\[.*\]//;
---> This shall search for "an opening parenthesis followed by zero or
more characters (.*) followed by a close parenthesis". So if the input
is
 
*MOT:   I'm gonna first [//] first I wanna use em all up .
then output will be:
*MOT:   I'm gonna first first I wanna use em all up .
 
[!]HOWEVER if there are more than one pair of '[]' then another problem
occurs.
Eg:
Input: I'm gonna first [//] second [//] third I wanna use em all up.
Output: I'm gonna first third I wanna use em all up.
 
*         as u can see the 2nd 'first' is missing. This is because of
the "greediness" of Perl which tries to match as much of the search
pattern as possible.
 
To solve this, we use the '?' operator. Thus, the correct search pattern
is
$_ =~ s/\[.*?\]//;
This will give the output: I'm gonna first second [//] third I wanna use
em all up.
 
To remove all such occurrences, use the global search:
$_ =~ s/\[.*?\]//g;
Use a similar approach for '()'.
 
Regards,
Adarsh
 
 
 

Reply via email to