[Cascavel-pm] Duvida expressao regular
Nelson Ferraz
nferraz em gmail.com
Quarta Agosto 22 12:29:00 PDT 2007
> So mais uma coisa pessoal...
> se eu tirar aquele ? aparentemente funcionou =... qual o objetivo do ? sendo
> que ja tem *??
Você *realmente* não leu o manual... :-(
perldoc perlre
By default, a quantified subpattern is "greedy", that is, it will match
as many times as possible (given a particular starting location) while
still allowing the rest of the pattern to match. If you want it to
match the minimum number of times possible, follow the quantifier with
a "?". Note that the meanings don't change, just the "greediness":
*? Match 0 or more times
+? Match 1 or more times
?? Match 0 or 1 time
{n}? Match exactly n times
{n,}? Match at least n times
{n,m}? Match at least n but not more than m times
Exemplo:
my $str = "xyzzy";
my ($match1) = $str =~ /xy(z*)/;
my ($match2) = $str =~ /xy(z*?)/;
if ($match1 eq $match2) {
print "iguais";
} else {
print "diferentes";
}
Veja se são iguais ou diferentes.
--
Nelson Ferraz
Free Software Foundation Associate Member #3203
Projeto Software Livre Brasil (www.softwarelivre.org.br)
Sociedade Perl do Brasil (www.perl.org.br)
Rede Livre de Compartilhamento de Cultura Digital
Mais detalhes sobre a lista de discussão Cascavel-pm