Forum Moderators: phranque

Message Too Old, No Replies

Which is better: /([^/]+)/ or /([0-9]+)/

I know (now) not to use /(.*)/

         

whoisgregg

2:55 pm on Apr 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



First, thanks jdMorgan for the excellent mod_rewrite tutorial [webmasterworld.com].

After reading the section about the `very inefficient construct "(.*)/(.*)"` I have a question about other methods. If the directory structure is limited to a specific character range, is it more or less efficient to specify those characters?

In other words, if I only allow numbers in a directory, which choice is more efficient:

/([^/]+)/

-OR-

/([0-9]+)/

I see a benefit to the latter because it is an extra "validation" step prior to the script being invoked, but if the script is validating anyways (as it should) can I save Apache processor cycles by switching to the former?

jdMorgan

3:18 pm on Apr 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't know, since I've never tested and compared those two patterns performance-wise.

But I think that either one would be about the same, and I'd opt for whichever you consider to be easiest to maintain. Your 'validation' concern is also a factor -- Do you want invalid requests passed to the script so that you can handle them 'elegantly' -- or to be rejected by the rewriterule, and allowed to throw a 404 error?

If, however, the the [0-9] needed to be expanded to [0-9a-z], then the advantage would go to [^/] because [0-9a-z] requires *two* range compares instead of one. Think about coding the pattern-matching routine in 'C' or a similar language, and performance-affecting factors become clearer.

Jim