builtin regular expressions?

Jorge Godoy jgodoy at gmail.com
Sat Sep 30 13:04:23 EDT 2006


Mirco Wahab <wahab at chemie.uni-halle.de> writes:

>   sub print_message {
>      if   (/^track="(.+?)"/ ){ print "Your track is $1\n" }
>      ...
>
> which has a "more complicated" regex that is usually
> not understood easily by newbies.

Specially the non-greedy part. :-) I don't believe that non-greedyness would
be adequate here since I believe he's willing to process the whole line.

===========================================================================
$line = "track='My favorite track'";
if ($line =~ /^track=(.+?)/) { print "My track is $1\n"};
===========================================================================

outputs

===========================================================================
My track is '
===========================================================================

While

===========================================================================
$line = "track='My favorite track'";
if ($line =~ /^track=(.+)/) { print "My track is $1\n"};
===========================================================================

outputs

===========================================================================
My track is 'My favorite track'
===========================================================================

and what I'd use

===========================================================================
$line = "track='My favorite track'";
if ($line =~ /^track=(.*)/) { print "My track is $1\n"};
===========================================================================

has the same output.  ;-)

All this running perl 5.8.8.


Be seeing you,
-- 
Jorge Godoy      <jgodoy at gmail.com>



More information about the Python-list mailing list