SPUG: regular expression question

Ryan T. Kosai rkosai at u.washington.edu
Mon Nov 14 23:07:12 PST 2005

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

$line =~ /^(([^#]|(?:(?<=\\)[#]))*)/;

print "$1\n";


Ryan T. Kosai, Undergraduate                     rkosai at u.washington.edu
Molecular Biology/Electrical Engr.      //students.washington.edu/rkosai

Charles Connolly wrote:
> Hi,
>      I'm having some trouble with a regular expression, and I thought I'd toss 
> it out to the list, especially since it seems quiet here lately. 
>      I'd like to process a file line by line, capturing the everything from the 
> start of the line to a hash mark (#), which would indicate a comment. Here are 
> some things I tried: 
> my $line =~ /^(.*)\#?/; # this takes the entire line, as expected. 
> my $line =~ /^(.*?)\#?/; # this works if there is a comment, but not if there 
> isn't. If there isn't a comment it captures the shortest match, which is 
> nothing.
> All suggestions are welcomed.
> Thanks,
> Chuck
> _____________________________________________________________
> 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/

More information about the spug-list mailing list