Shimon Bollinger wrote: > In the following program, I want the regex to always match what's to the > left of the alternation operator ($larry) before matching 'default'. > Because of the leftmost rule, this doesn't happen; in the second case, > 'default' matches because it appears earlier in the string. How can I > modify the regex so that the first match prints 'foo' and the second > match prints 'bar'? > > Thanks for your help. > > #!/usr/bin/perl -w > use strict; > my ($larry, $match); > > my ($str) = <<EOQ; > foo > default > bar > EOQ > > $larry = 'foo'; > ($match) = $str =~ /($larry|default)/; > print "$match\n"; # prints foo > > $larry = 'bar'; > ($match) = $str =~ /($larry|default)/; > print "$match\n"; # prints default
( $match ) = $str =~ /($larry)/ ? $1 : $str =~ /(default)/; John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>