...

View Full Version : is this regex succinct enough?



bazz
04-19-2009, 02:39 AM
Hi,

I wonder if this regex could be shortened and remain effective.



if ($date =~ /^(\d{4})-(\d{2})-(\d{2})$/ )
{
print qq( the date format is yyyy-mm-dd );
}

Would you do it like that or would you just make sure it contained digits and '-' ?

bazz

KevinADC
04-19-2009, 08:13 AM
If all you need to do is check the format then drop the parentheses as that forces perl to store the matches in memory ($1, $2, etc) which just slows things down.




if ($date =~ /^\d{4}-\d{2}-\d{2}$/ )
{
print qq( the date format is yyyy-mm-dd );
}


or without the block:



print qq( the date format is yyyy-mm-dd ) if ($date =~ /^\d{4}-\d{2}-\d{2}$/ );


Some people don't like to write code like that but I see no problem with it as long as the line is fairly short.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum