Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Perl Beginners > RE: problem usi...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 4 of 5 Topic 10975 of 11531
Post > Topic >>

RE: problem using backslash on brackets in regular expressions

by adarsh.s85@[EMAIL PROTECTED] (Adarsh S85) Apr 23, 2008 at 05:28 AM

------=_NextPart_000_0001_01C8A502.EC934050
Content-Type: text/plain;
	charset="us-ascii"
Content-Transfer-Encoding: 7bit

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
 
 
 

------=_NextPart_000_0001_01C8A502.EC934050--
 




 5 Posts in Topic:
Joining/Merging AoA
v3gupta@[EMAIL PROTECTED]  2008-04-21 23:28:58 
problem using backslash on brackets in regular expressions
danmcclory@[EMAIL PROTECT  2008-04-22 19:05:39 
Re: problem using backslash on brackets in regular expressions
rvtol+news@[EMAIL PROTECT  2008-04-23 01:22:06 
RE: problem using backslash on brackets in regular expressions
adarsh.s85@[EMAIL PROTECT  2008-04-23 05:28:50 
RE: problem using backslash on brackets in regular expressions
"Wagner, David --- S  2008-04-22 18:17:20 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Jul 26 1:02:50 CDT 2008.