SPUG: regular expression question

Rick Croote rick.croote at philips.com
Fri Nov 18 10:57:10 PST 2005


spug-list-bounces at pm.org wrote on 2005-11-15 05:46:34 AM:

> Ryan T. Kosai wrote:
> > Here's an alternative regex that does what I think you might actually 
want:
> > 
> > #This one catches escaped # signs
> > #  Briefly, Capture anything other than a #, OR capture a # if there's
> > #  a \ behind it
> 
> Did you mean behind it like #\ or in front of it like \#?
> 
> 
> > $line =~ /^(([^#]|(?:(?<=\\)[#]))*)/;
> 
> It looks like you have one too many sets of capturing parentheses, 
perhaps you
> meant something like this:
> 
> $line =~ /^((?:[^#]|(?<=\\)#)*)/;
> 
> 
> And of course the question is moot if the OP is using this on perl code 
as the
> # character may not involve a comment, for example:
> 
> my @array = qw# one two three #;
> 
> my $string = sprintf '%#x  %#x', 1234, 5678;
> 
> 
> 
> John
> -- 
> use Perl;
> program
> fulfillment
> _____________________________________________________________
> Seattle Perl Users Group Mailing List 
>      POST TO: spug-list at pm.org
> SUBSCRIPTION: http://mail.pm.org/mailman/listinfo/spug-list
>     MEETINGS: 3rd Tuesdays, Location: Amazon.com Pac-Med
>     WEB PAGE: http://seattleperl.org/

Great point John.  I've done this type of project, like most of us I'm 
sure, and always found myself analyzing on a char at a time basis, because 
you need to know if you are in a comment, multi-line comment, quoted 
string and who knows what else you will run into.  I found that line at a 
time parsing was too difficult for this type of project. 

But in the interest of fun, if I were not concerned about embedded comment 
characters and just wanted to eliminate them line by line, I always prefer 
the trimming style, rather than the capture.

$line =~ s/#.*//;  # Now isn't this "simple"



---
Rick Croote
Software Engineer
Environment and Tools Team
Philips Medical Systems
Bothell, WA
Rick.Croote at Philips.com
Phone: 425-487-7834


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.pm.org/pipermail/spug-list/attachments/20051118/d693d883/attachment.html


More information about the spug-list mailing list