regex - regular expression doesn't match, why? -


i want match paths that:

don't start "/foo-bar/"

or not ends extension (.jpg, .gif, etc)

examples:

/foo-bar/aaaa/fff not match  /foo-bar/aaaa/fff.jpg not match  /aaa/bbb match  /aaaa/bbbb.jpg not match  /bbb.a not match 

this regex:

^\/(?!foo-bar\/).*(?!\.).*$ 

but not working, why?

thanks!

it more easy try match don't want. example php:

if (!preg_match('~^/foo-bar/|\.[^/]+$~', $url))     echo 'valid!'; 

your pattern doesn't work because of part .*(?!\.).*$. first .* greedy , take characters of string until end, after, make end of pattern succeed, regex engine backtrack 1 character (the last of string). (?!\.).*$ match last character if not dot.

if absolutely need affirmative pattern, can use this:

if (preg_match('~^/(?!foo-bar/)(?:[^/]*/)*+[^./]*$~', $url))     echo 'valid!'; 

Comments

Popular posts from this blog

c++ - How to add Crypto++ library to Qt project -

jQuery Mobile app not scrolling in Firefox -

How to use vim as editor in Matlab GUI -