felix001
felix001

Reputation: 16691

MySQL Regex for excluding string

Im trying to perform a regex match in mysql to match a string when part of the string is not present. i.e

src="/images/(?legacy)

if the string src="/images/ is present but the string legacy is not. Any ideas ?

Upvotes: 0

Views: 601

Answers (2)

thelogix
thelogix

Reputation: 610

I know this is not what you're asking, but MySQL regexes do not support negative lookahead and AFAIK mysql regexes dont use the index.

So i think you may be over thinking it, as the regex would be shorter in writing (if it could work, which it cant) but traditional LIKE is faster.

I suggest you simply use:

WHERE (
   content LIKE '%src="/images/%' 
   AND 
   content NOT LIKE '%src="/images/legacy%'
)

Upvotes: 1

Explosion Pills
Explosion Pills

Reputation: 191749

MySQL regexen do not support negative lookaheads, so I think your best bet would be to use two or to use LIKE

string LIKE '%src="/images/%'
AND string NOT LIKE '%src="/images/legacy%'

Note that this query would be inefficient (as it would be using regexen).

Upvotes: 1

Related Questions