quinique
quinique

Reputation: 23

Negative Lookahead Mistake

I want to get these strings

84fyctvvtvxy.jpg
84fyctv_vtv(test)-_xy.jpg

but not these strings

84fyctvvtvxy_t.jpg
84fyctvvtv(test)-_xy_t.jpg

I tried this RegEx

([A-Za-z0-9 ()€_]+(?!(.|.)_t))\.jpg

I need to exclude string that ends with _t.jpg

But it doesn't work can anyone help please?

Upvotes: 2

Views: 43

Answers (2)

Paolo
Paolo

Reputation: 26014

You may use the following pattern:

^(?!.*_t\.jpg$)[\w()€ .-]+$
  • ^ Assert beginning of line.
  • (?!.*_t\.jpg$) Negative lookahead, checks that substring is not at end of string.
  • [\w()-.]+ Match any character in the character set.
  • $ Assert position end of line.

You can try it here.

Upvotes: 2

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726479

You can solve this problem by using a negative lookbehind (?<!_t) for the \.jpg part of the pattern, like this:

([A-Za-z0-9 ()€_-]+)(?<!_t)\.jpg

Demo.

Upvotes: 1

Related Questions