J
Jon Combe
Can anyone explain why the following code is not doing what I expect
it to do ?
#!/usr/bin/perl -w
use strict;
my $inputDateFormat = "DD-MM-YY";
if ( $inputDateFormat =~ m/YY/g )
{
print "GOT YEAR \n";
}
print "ABOUT TO MATCH WITH [$inputDateFormat]\n";
if ( $inputDateFormat =~ m/MM/g )
{
print "GOT MONTH\n";
}
The output I get is
GOT YEAR
ABOUT TO MATCH WITH [DD-MM-YY]
I would expect it to also output "GOT MONTH"
With some expermentation I have found that removing the "g" modifier
from the end of the second match gives the correct result, but once
matched I want to use the "pos" function which seems to only work
correctly when you have the g modifier.
Oddly if I also do a match for DD:-
#!/usr/bin/perl -w
use strict;
my $inputDateFormat = "DD-MM-YY";
if ( $inputDateFormat =~ m/YY/g )
{
print "GOT YEAR \n";
}
print "ABOUT TO MATCH MONTH WITH [$inputDateFormat]\n";
if ( $inputDateFormat =~ m/MM/g )
{
print "GOT MONTH\n";
}
print "ABOUT TO MATCH DAY WITH [$inputDateFormat]\n";
if ( $inputDateFormat =~ m/DD/g )
{
print "GOT DAY\n";
}
I get output
GOT YEAR
ABOUT TO MATCH MONTH WITH [DD-MM-YY]
ABOUT TO MATCH DAY WITH [DD-MM-YY]
GOT DAY
So it matches DD but still won't match MM.
I've tried on perl 5.8.0, 5.8.7 and 5.6.1 with identical results.
I'm not sure if this is a bug or some subtle behaviour of regular
expressions I'm not aware of, but it certainly seems odd to me.
Also if I don't do the check for "YY" first it then matches MM but
fails to match DD
Jon
it to do ?
#!/usr/bin/perl -w
use strict;
my $inputDateFormat = "DD-MM-YY";
if ( $inputDateFormat =~ m/YY/g )
{
print "GOT YEAR \n";
}
print "ABOUT TO MATCH WITH [$inputDateFormat]\n";
if ( $inputDateFormat =~ m/MM/g )
{
print "GOT MONTH\n";
}
The output I get is
GOT YEAR
ABOUT TO MATCH WITH [DD-MM-YY]
I would expect it to also output "GOT MONTH"
With some expermentation I have found that removing the "g" modifier
from the end of the second match gives the correct result, but once
matched I want to use the "pos" function which seems to only work
correctly when you have the g modifier.
Oddly if I also do a match for DD:-
#!/usr/bin/perl -w
use strict;
my $inputDateFormat = "DD-MM-YY";
if ( $inputDateFormat =~ m/YY/g )
{
print "GOT YEAR \n";
}
print "ABOUT TO MATCH MONTH WITH [$inputDateFormat]\n";
if ( $inputDateFormat =~ m/MM/g )
{
print "GOT MONTH\n";
}
print "ABOUT TO MATCH DAY WITH [$inputDateFormat]\n";
if ( $inputDateFormat =~ m/DD/g )
{
print "GOT DAY\n";
}
I get output
GOT YEAR
ABOUT TO MATCH MONTH WITH [DD-MM-YY]
ABOUT TO MATCH DAY WITH [DD-MM-YY]
GOT DAY
So it matches DD but still won't match MM.
I've tried on perl 5.8.0, 5.8.7 and 5.6.1 with identical results.
I'm not sure if this is a bug or some subtle behaviour of regular
expressions I'm not aware of, but it certainly seems odd to me.
Also if I don't do the check for "YY" first it then matches MM but
fails to match DD
Jon