#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2009
    Posts
    37
    Rep Power
    16

    Matching two strings, one with slight variation


    i'm trying to match two strings with a single regex. they are both very similar; the difference being an additional ten character sequence added in the second one (being iu503CsuD4 in this example):

    1_3718_1278481805_7507285_m.jpg
    1_3718_1278481805_7507285_iu503CsuD4_m.jpg

    to match them i have these two regular expressions, respectively:

    /^(.+)(t|s|m|l|o)(\.jpg|jpeg)$/i
    /^(.+)([a-z0-9]{10}_)(t|s|m|l|o)(\.jpg|jpeg)$/i

    is there a way to combine those expressions to match both strings? i was trying to use the "?" operator on the second expression to make the additional ten character sequence optional, but it doesn't work.

    all responses will be appreciated.
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2009
    Posts
    23
    Rep Power
    0
    Not 100% positive,but try this:
    /^(.+)([a-z][a-z0-9]{9}_)?(t|s|m|l|o)(\.jpg|jpeg)$/i

    The difference is that since the third 'phrase' in
    1_3718_1278481805_7507285_iu503CsuD4_m.jpg
    is 10 chars also (1278481805), so what I thought to do is first look for a char that's not a digit and then grab 9 numbers afterward and make that optional.

    I'm not sure if it'll work, but it's worth a shot.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2009
    Posts
    37
    Rep Power
    16
    cali, that unfortunately won't be good; the 10 character sequence does not always start with a letter. i'm also sure that the earlier 10 character sequence of pure numbers will not interfere with the 10 character mixed sequence of letters and numbers.

    thank you, though! if you're struck by anything else, do let me read it.

IMN logo majestic logo threadwatch logo seochat tools logo