regular express : how to exclude multiple character groups?

regex

Solution

What you need here is a negative lookahead assertion. What you want to say is "I want to match any string of characters, except for these particular strings." An assertion in a regex can match against a string, but it doesn't consume any characters, allowing those character to be matched by the rest of your regex. You can specify a negative assertion by wrapping a pattern in `(?!` and `)`.

'/^\/(?!products|categories|admin)(.+)$/'

Note that you might want the following instead, if you don't allow customers names to include slashes:

'/^\/(?!products|categories|admin)([^/]+)$/'

Problem

I have a set of urls : /products /categories /customers Now say a customers is named john, and I want to help john to reach his own account page with a shorter url: ``` before : /customers/john after : /john ``` (suppose customer names are unique) I'm trying to figure out a proper regex dispatcher so all customers can have this feature : ``` /marry /james /tony-the-red-beard ``` here is what I got now(in PHP) : ``` '/^\/([^(products|categories|admin)].+)$/' => /customers/$1 ``` This doesn't seem to work. Anybody can help me?

Original source