1

I am wanting to match files that have not already got thumbnails created.

So, for an image that has had two thumbnails created:

image1-100x100.png
image1-350x350.png
image1.png

I am looking for some regex to match only image1.png.

What I have at the moment selects the extensions only:

(?!(-(100x100|350x350)))(\.(jpg|png))

3 Answers 3

3

Try this:

^(?!.*-(?:100x100|350x350)).*\.(?:jpg|png)$

Explanation:

^                     # Start of string
(?!                   # Assert that it's impossible to match...
 .*                   # any string, followed by
 -                    # a dash, followed by
 (?:100x100|350x350)  # 100x100 or 350x350
)                     # End of lookahead.
.*                    # Then match any string
\.                    # followed by a dot
(?:jpg|png)           # and a jpg/png extension.
$                     # End of string
Sign up to request clarification or add additional context in comments.

5 Comments

What if your filename is image1-200x200.png?
Clever you. Well, usually the thumbnail sizes are more standardized than the filenames, but of course if they are variable, then that needs to be taken into account. The rules have not been defined clearly enough to do more than guess.
Agreed, it is one of those questions with incomplete and vague spelled out requirements.
The thumbnail sizes will only be 100x100 or 350x350. My system create these thumbnails automatically on upload, with filters in place to rename the files to remove these values if they exist.
What if the filename contains -100x100 or -350x350 somewhere in the middle?
2

One or more non-hyphens, followed by .jpg or .png:

^[^-]+\.(?:jpg|png)$

No need for negative lookahead. (Note that your existing regex is actually equivalent to just (\.(jpg|png)): the lookahead has no real effect, since no string could have (\.(jpg|png)) as a prefix and (-(100x100|350x350)) as a prefix.)


Edited to add: Your question seems somewhat self-contradictory, in that you initially say that you are "wanting to match files that have not already got thumbnails created", but then in your example, you say that you want to match image1.png even though it has had two thumbnails created.

You also, in my opinion, don't give clear rules for how to determine if an image is a thumbnail. Above, I took the simplest approach, which is to assume that thumbnails' filenames contain hyphens and other images' filenames do not. Alternatively, we might take the very narrowest definition of a thumbnail image, and say that "a file is a non-thumbnail image if its filename ends in a .jpg or .png that is not preceded by -100x100 or -350x350"; in that case, we can write:

^.*(?<!-100x100)(?<!-350x350)\.(?:jpg|png)\z

using negative lookbehind . . . assuming a regex-engine that supports negative lookbehind. (You don't mention what language you're using?) Without negative lookbehind, we can instead write:

^(?:[^-]|-(?!(?:100x100|350x350)\.(?:jpg|png)\z))*\.(?:jpg|png)\z

but then it's much simpler to just use two regexes:

\.(?:jpg|png)\z
-(?:100x100|350x350)\.(?:jpg|png)\z

and require that the string not match the second. Your question implies that you want to do this as a single affirmative regex, but you don't mention why?

3 Comments

heh, quite obvious :) was just about to post the same answer
What if your filename is new-image.png?
@TimPietzcker: Then obviously it's a thumbnail, with dimensions image, for an image named new.png. Duh! (O.K., more seriously: my personal policy, when constructing a regex for which the requirements are not clear, is to choose the simplest regex that satisfies plausible requirements, and then document what it does. If it turns out that those requirements are not sufficient, then if nothing else, I've helped the OP to flesh out the requirements.)
1

^(.+)(?!(-((\d+)x(\d+))))(\.(jpg|png))$

1 Comment

Sorry, but that's wrong. (?!...) doesn't work the way you think it does.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.