Results 1 to 8 of 8

Thread: [RESOLVED] Regex help please

  1. #1

    Thread Starter
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,427

    Resolved [RESOLVED] Regex help please

    I have a slideshow application that uses regex to exclude certain pictures based on the picture's file path. It excludes thumbnails with .*/thumb*

    How can I modify that to exclude both thumbnail and /derivative

  2. #2
    PowerPoster
    Join Date
    Aug 2010
    Location
    Canada
    Posts
    2,412

    Re: Regex help please

    That regex looks a bit fishy to me. It says match any character 0 or more times, and then match a forward slash, then match the "thum" followed by the letter "b" 0 or more times.

    Are you on a Windows or Linux filesystem? Or are the paths URLs?
    Are you trying to match any folders that are exactly "thumbnail" or "derivative"?
    Would you want to match folders named "thumb", "thumbnail", "thumbnails", "thumbelina", etc...?
    What about a folder named "green_thumb"?
    What if there's a file named "thumb.jpg" in the "C:\thumbnail" folder? Should it be included or excluded?

  3. #3

    Thread Starter
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,427

    Re: Regex help please

    • Are you on a Windows or Linux filesystem?==> macOS
    • Or are the paths URLs?==> No they are file paths
    • Are you trying to match any folders that are exactly "thumbnail" or "derivative"==> I want to exclude things like blah/thumbnails/Spain and blah/derivatives/Peru
    • What about a folder named "green_thumb"?==>Not a possibility
    • What if there's a file named "thumb.jpg" in the "C:\thumbnail" folder? Should it be included or excluded?==>Any path with /thumbnail in it should be eliminated. And the .*/thumb* does that.

  4. #4
    PowerPoster
    Join Date
    Aug 2010
    Location
    Canada
    Posts
    2,412

    Re: Regex help please

    OK, I wanted to clarify whether you should be using forward or backslashes. MacOS uses forward slash for path separators, so thanks for the clarification.

    */thumb will exclude a lot more than just folders named "thumbnail". It will also exclude file names that start with "thumb". I think this will work better:

    (\/thumbnail(s?)(\/|$)|\/derivative(s?)(\/|$))

    That will match folders named "thumbnail", "thumbnails", "derivative", and "derivatives".

  5. #5
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,120

    Re: Regex help please

    Why escape the forward slash? (Edit: I guess it makes sense escaping it in any language with regex literals like Perl or JS)

    A somewhat more explicit regex:

    /(thumbnail|thumbnails|derivative|derivatives)(/|$)

    Hope it's obvious how to include more directories in the check.

    cheers,
    </wqw>

  6. #6
    PowerPoster
    Join Date
    Aug 2010
    Location
    Canada
    Posts
    2,412

    Re: Regex help please

    Quote Originally Posted by wqweto View Post
    Why escape the forward slash? (Edit: I guess it makes sense escaping it in any language with regex literals like Perl or JS)
    Yeah, I'm one of those weirdos that uses Perl a fair bit, so force of habit I guess.

    I was also testing with some random online regex tester that must be broken (or using a weird dialect) because I initially tried putting the tests for the path separators/end of line outside the pattern group and it wasn't matching. Either that or I was still half asleep.

  7. #7
    PowerPoster
    Join Date
    Aug 2010
    Location
    Canada
    Posts
    2,412

    Re: Regex help please

    Here's another attempt that's a bit more concise (at the expense of some readability):

    Code:
    /(thumbnails?|derivatives?)(/|$)

  8. #8

Posting Permissions

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



Click Here to Expand Forum to Full Width