Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts

    is this regex succinct enough?

    Hi,

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

    Code:
    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
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link

  • #2
    Senior Coder
    Join Date
    Mar 2006
    Posts
    1,274
    Thanks
    2
    Thanked 39 Times in 38 Posts
    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.


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

    Code:
     
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •