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 5 of 5
  1. #1
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts

    checking regex is good

    Hi,

    my image files are labelled like this:

    image01.jpg or image234.jpg or image01.gif or image01.png

    The differences are in the numbers or the extension.

    This regex appears to work:

    Code:
        next unless ($image_no =~ /^(image)(\d+)(.jpg|.gif|.png)+$/);
    Not sure if it should be that or something like

    Code:
        next unless ($image_no =~ /^(image)(\d+).(jpg|gif|png)+$/);
    How can we measure the greediness or other efficiency of a regex?
    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
    Regular Coder
    Join Date
    Jun 2003
    Location
    Silicon Forest
    Posts
    155
    Thanks
    0
    Thanked 5 Times in 5 Posts
    I don't know any good ways to test efficiency but I can help make your regex a little better since it has some issues. With your second regular expression you would accept "image123_pnggifjpg" which probably isn't what you were expecting.
    Code:
    /^image(\d+)\.(jpg|gif|png)$/
    You need to escape the . since that represents all characters (except newline), not the period that you were looking for. Also the + after the (jpg|gif|png) was unneccesary, since you expect one file extension.

    One thing for efficiency (or memory overhead at least) is that if you don't need the values for the capture groups, you can say not to store them with ?: for example (?:\d+). I can't say exactly how much it helps, if you really want to know everything about perl regex's read http://perldoc.perl.org/perlre.html
    Last edited by CrzySdrs; 02-18-2010 at 07:45 PM.
    Whats the point of a signature?

  • #3
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,948
    Thanks
    2
    Thanked 170 Times in 165 Posts

  • #4
    New Coder
    Join Date
    Mar 2009
    Posts
    90
    Thanks
    5
    Thanked 14 Times in 14 Posts
    Hi,my suggestion is:
    Code:
    /^image(\d+)\.(jpe?g|gif|png)$/i
    testing jpg/jpeg and i modifier

  • #5
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts
    Thank you all. I'll take a read at yape and perldoc.

    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


  •  

    Posting Permissions

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