Any idea why this regex doesnt work on Symfony Routing

  symfony, symfony-routing, symfony4

I have this regex setup
((^(?!edit/?$)^(?!recruiters/?$).*))

@Route("/{type}/{slug}", name="route-name", requirements={"slug"="(^(?!term/?$)^(?!anotherterm/?$).*)" })

My goal is that if slug is "term" or "anotherterm" it doesnt match this route.
I have used different regex tools and it works fine, I have also tried testing using preg_match() and it works fine. Other tools I used are below and also https://www.debuggex.com/ and https://regex101.com/r/COBJ70/2

You can check here https://regexr.com/5eidd with a several automated tests and they all work only in symfony it doesnt

Results using preg_match

var_dump(preg_match("(^(?!term/?$)^(?!anotherterm/?$).*)", 'term'));
var_dump(preg_match("(^(?!term/?$)^(?!anotherterm/?$).*)", 'random-text'));

int(0)
int(1)

And this is the results I get from symfony

console router:match /something/random-text

 Route "router-name" almost matches but requirement for "slug" does not match ((^(?!term/?$)^(?!anotherterm/?$).*))

Doing some in-deptch research I noticed that the regex gets transformed by symfony to
#^/(?P<type>[^/]++)/(?P<slug>(?:^(?!term/?$)^(?!anotherterm/?$).*))$#sD on /symfony/routing/Route.php::compile() and then a preg_match is performed and doesnt match.

Source: Symfony Questions

LEAVE A COMMENT