News for hackers. Stuff that matters.
Re:Fast request routing using regular expressions by mzs on Thursday February 20, 2014 @02:06PM (#7272866)
Not quite what you asked for, but how about this?

  ~^(?|
      xx(x)/user/([^/]+)/(\d+)
    | x(xx)/user/(\d+)
    | (xxx)/user/([^/]+)
  )$~x
Then just prepend 'xxx' to the front of uri in the call to preg_match. Now the second entry in matches tells you which handler, so for example for uri = '/user/nikic/42' you pass 'xxx/user/nikic/42' and get back 'xxx/user/nikic/42', 'x', 'nikic', and '42' where that 'x' tells you it's the first handler.

I'm not an expert in how php does REs but I imagine there may be a way to use preg_replace or similar to replace the first '/' in the uri with 'x', 'xx', or 'xxx' and still get a the groups returned for the matches for user and id say. This could save a potentially costly string creation.

There are no comments on this comment yet.