3

I need to choose the original image in my example is a 'image1.jpg' with any extension.

image1-150x150.jpg
image1-225x300.jpg
image1-768x1024.jpg
image1.jpg

I've tried the following:

preg_grep("/-([0-9]+x[0-9])+/", $image);

How do I exclude other files and select "image1.jpg"?

2
  • 1
    Could you add a little context please? Commented Jan 1, 2016 at 19:48
  • thanks. it's works, but I wanted to write without PREG_GREP_INVERT. because it will be used in some cases without preg_grep Commented Jan 1, 2016 at 19:54

1 Answer 1

2

I wanted to write without PREG_GREP_INVERT, because it will be used in some cases without preg_grep

Then you could use a negative lookahead to negate strings that contain the pattern \d+x\d+:

/^((?:.(?!\d+x\d+))*\.[^.]+)$/gm

It would capture image1.jpg based on the input you provided - example here.

  • ^ - Anchor to match the beginning of the line (since the m flag is used).

  • .(?!\d+x\d+))* - Match zero or more characters not followed by \d+x\d+

  • \.[^.]+ - Match the . character literally followed by one or more characters that are not .

  • $ - Anchor to match the end of the line.

Sign up to request clarification or add additional context in comments.

1 Comment

It works. Thanks for the detailed description.Thanks to all who responded. Happy New Year

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.