
kencl
User
Aug 13, 2005, 4:45 PM
Post #1 of 1
(1108 views)
|
|
use of /o modifier with qr//
|
Can't Post
|
|
Hi folks, I'm just a little unsure of where the /o modifier should be used.
# match positive and negative integers and reals use strict; my @numbers = my @strings = (); my @test_values = qw(abc 1 -2 3.3 -4.4 5. -6. .7 -.8); my $numcheck_pattern = qr/^\-?(?:\d+(?:\.\d+)?|\d+\.|\.\d+)\z/o; my $string = ''; foreach $string(@test_values) { if ($string =~ /$numcheck_pattern/o) { # Is this /o necessary or redundant? push(@numbers, $string); } else { push(@strings, $string); } } print 'strings: "' . join(', ', @strings) . qq|"\n|; print 'numbers: "' . join('" "', @numbers) . qq|"\n|; __END__ # output strings: "abc" numbers: "1" "-2" "3.3" "-4.4" "5." "-6." ".7" "-.8" When the /o modifier is used to quote the pattern (qr/.../o), is it necessary to also use it when applying the regex? Thanks. PS, here's the pattern broken down in case you want to understand how it works:
my $numcheck_pattern = qr/^ # absolute beginning of string \- # negative sign ? # quantify negative sign as optional ( # contain sub expression to dilineate alternatives ?: # don't capture match into \1 or $1 # first alternative matches eg "1" or "1.1" \d+ # one or more integer chars ( # contain sub expression for benefit of "?" quantifier ?: # don't capture match into \2 or $2 \. # decimal point \d+ # one or more integer chars ) # end second sub expression containment ? # quantify decimal portion as optional | # second alternative matches eg "1." \d+ # one or more integer chars \. # decimal point | # third alternative matches eg ".1" \. # decimal point \d+ # one or more integer chars ) # end first sub expression containment \z # absolute end of string /xo; # extended, only compile once >> If you can't control it, improve it, correlate it or disseminate it with PERL, it doesn't exist!
|