Sunny
Sunny

Reputation: 227

PHP preg_match exclude

OK this regex will match string like 2aa, a2, 2aaaaaa, aaaa2, aaa2aaaa, 2222a2222-2222-aaaa... in short, mix of alphanumeric characters in a sequence:

preg_match("/(?:\d+[a-z]|[a-z]+\d)[a-z\d]*/i")

now I want to exclude something but I'm stuck, something like this doesn't work

preg_match("/(?!1920x1200|1920x1080)(?:\d+[a-z]|[a-z]+\d)[a-z\d]*/i")

for example the string aaaaa222aaa1920x1200bbbbb1234556789 is still matched but it shouldn't because it contains 1920x1200

any help is appreciated :)

i'm using regex found here for matching alphanum sequences Regex: match only letters WITH numbers

regex test: https://regex101.com/r/vU9aU9/1

Upvotes: 0

Views: 2336

Answers (1)

anubhava
anubhava

Reputation: 784958

Your negative lookahead should have .* in front to allow for 0 or more characters before not-allowed text. Also use anchors in your regex.

regex should be:

preg_match('/^.*?1920x1200.*$(*SKIP)(*F)|(?:\d+[a-z]|[a-z]+\d)[a-z\d]*/im')

RegEx Demo

Upvotes: 3

Related Questions