[ale] Perl Regexe questions

Jason Day jasonday at worldnet.att.net
Wed Jun 16 13:29:32 EDT 2004


On Tue, Jun 15, 2004 at 06:38:18PM -0400, Christopher Fowler wrote:
> 
> 
> I'm trying to strip the file name from the following string:
> 
> /opt/SAM/SystemConfig/vtun.pl
> 
> I'm using the following regext:
> 
> m/\/(.+)/;

Most regular expressions are ambiguous.  This one just specifies a "/"
followed by one or more characters.  Since there are 4 "/" characters
followed by other characters in the input text, there are 4 ways that
this regexp can match:

opt/SAM/SystemConfig/vtun.pl
SAM/SystemConfig/vtun.pl
SystemConfig/vtun.pl
vtun.pl

By default, subpatterns in perl regexps are "greedy", which means they
will attempt to match as many characters as possible.  This gives the
first match above.

What you really want though is a regexp that specifies a "/" character,
followed by one or more characters that are not a "/":

m{/([^/]+)$}

HTH,
Jason
-- 
Jason Day                                       jasonday at
http://jasonday.home.att.net                    worldnet dot att dot net
 
"Of course I'm paranoid, everyone is trying to kill me."
    -- Weyoun-6, Star Trek: Deep Space 9



More information about the Ale mailing list