regex - Perl Regular Expression default for non-matched -


lets this:

my ($a,$b,$let) = $version =~ m/^(\d+)\.(\d+)\.?([a-za-z])?$/; 

so match instance: 1.3a, 1.3,... want have default value $let if let not available, lets say, default 0. 1.3 get: $a = 1 $b = 3 $let = 0

is possible? (from regex self, without using additional statements)

thanks,

this work - updated use bitwise or instead of ternary operator.

my ($a,$b,$let) = ($version =~ m/^(\d+)\.(\d+)\.?([a-za-z])?$/)              && ($1,$2,$3 || 0 ); 

here test script

&t("1.3"); &t("1.3a"); &t("1.3.a");  sub t { $version = shift; ($a,$b,$let) = ($version =~ m/^(\d+)\.(\d+)\.?([a-za-z])?$/)                  && ($1,$2,$3 || 0 ); print "\n result $a.$b.$let"; } 

output

result 1.3.0 result 1.3.a result 1.3.a 

original solution using ternary operator works

my ($a,$b,$let) = ($version =~ m/^(\d+)\.(\d+)\.?([a-za-z])?$/)              && (defined $3 ? ($1,$2,$3) : ($1,$2,0)); 

Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -