APM: Regular Expression Question

Tim Peoples tim at toolman.org
Wed Aug 16 09:03:50 PDT 2006


I think what you're looking for is something like this:

    $cnt = 6;
    $piece =~ s/(?<!\")\t/\"\t\"/ while $cnt--;

This will substitute the first six tabs with quoted tabs.  The (?<!\") 
business is a "/zero-width negative look-behind assertion/" that says it 
should only match tab chars that are not immediately preceded by a quote 
char (but the quote char is not included in the text to get 
substituted).  since there is no /g modifier on the substitution, we 
have to do it all with an in-line while loop constrained by $cnt.

Tim.

Barron Snyder (CE CEN) wrote:
>
> $piece =~ s/\t/\"\t\"/g;
>
>  
>
> With the above code I'm searching for all tab characters and replacing 
> them with a tab surrounded by double-quotes. Is there a way without 
> putting this in a coding loop, to replace say, only the first three 
> instances it finds in $piece? It seems like a nice way to do this 
> would to be able to replace the "g" with an integer indicating how 
> many replacements to do but it doesn't seem to work like that <grin>.
>
>  
>
> I'm parsing some columnar data (7 columns) and I want to put 
> double-quotes around the first six columns. I've figured out a way to 
> do it but it's kind of clumsy. I thought you experts may be able to 
> show a Perl newbie a more elegant way to do it.
>
>  
>
> Thanks,
>
>  
>
> Barron Snyder
>
> Software Development, Financial Systems Team
>
> *Whole Foods Market, HQ*
>
> barron.snyder at wholefoods.com <mailto:barron.snyder at wholefoods.com>
>
>  
>
> This email contains proprietary and confidential material for the sole 
> use of the intended recipient. Any review, use, distribution or 
> disclosure by others without the permission of the sender is strictly 
> prohibited.  If you are not the intended recipient (or authorized to 
> receive for the recipient), please contact the sender by reply email 
> and delete all copies of the message.
>
>  
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Austin mailing list
> Austin at pm.org
> http://mail.pm.org/mailman/listinfo/austin

-- 
 _______________________________________________________________________
                                                      Timothy E. Peoples
                                                   Have Camel, Will Code
                                                         tim at toolman.org




This message and any attachments are intended only for the use of the addressee and may contain information that is privileged and confidential. If the reader of the message is not the intended recipient or an authorized representative of the intended recipient, you are hereby notified that any dissemination of this communication is strictly prohibited. If you have received this communication in error, please notify us immediately by e-mail and delete the message and any attachments from your system.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.pm.org/pipermail/austin/attachments/20060816/d64b9218/attachment.html 


More information about the Austin mailing list