builtin regular expressions?
Mirco Wahab
wahab at chemie.uni-halle.de
Sat Sep 30 15:02:29 EDT 2006
Thus spoke Jorge Godoy (on 2006-09-30 19:04):
> Mirco Wahab <wahab at chemie.uni-halle.de> writes:
>> sub print_message {
>> if (/^track="(.+?)"/ ){ print "Your track is $1\n" }
>> ...
>
> 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.
Ohh, but I think it really is, my intention was
to get the quotet text out of the quotes, if
there is any, eg.:
sub print_message {
if (/^track="(.+?)"/ ){ print "Your track is $1\n" }
...
}
print_message for 'track="My favorite track"', 'title="My favorite song"',
'artist="Those Dudes"', 'Something else' ;
(... our quoting chars are just inverted.)
> $line = "track='My favorite track'";
> if ($line =~ /^track=(.+?)/) { print "My track is $1\n"};
>
> outputs
> My track is '
Of course, you can't have the nongreedy thing
without a following item, in the case mentioned,
a second \" (which would have been consumed
in the 'greedy' mode).
> and what I'd use
>
> $line = "track='My favorite track'";
> if ($line =~ /^track=(.*)/) { print "My track is $1\n"};
OK, but to pull the quoted text alone, you'd
need the non-greedy thing, as in
...
if ( /^track='(.+?)'/ ){ print "Your track is $1\n" }
...
Alternatively, you could use the negated character class
for that:
if ( /^track='([^']+)/ ){ print "Your track is $1\n" }
which has exactly the same character count (so taste matters here) ...
Regards
Mirco
More information about the Python-list
mailing list