Forum Moderators: phranque

Message Too Old, No Replies

Shorter way of writing dates in .htaccess file

         

otem

1:21 pm on Apr 10, 2007 (gmt 0)

10+ Year Member



I'm creating a .htaccess rewrite rule of the url contains a valid date.

While it works just fine, my code seems longer than it needs to be.

I'm sure it doesn't make it any less efficient, but the long lines of code does make it a pain when trying to edit them.

Can these be made shorter?

My notes are on the left side of the colon, my modrewrite is on the right.

Year (2000+): (2[0-9]{3})

Month (01-12): (0[1-9]{1}¦1[0-2]{1})

Day (01-31): (0[1-9]{1}¦[12]{1}[0-9]{1}¦3[0-1]{1})

Thanks

jdMorgan

1:44 pm on Apr 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The default 'count' for any character-specific subpattern is one. It is therefore unnecessary to use the quantifier "{1}" on anything.

In groups, you can specify either ranges or alternates, so a group of [0-1] is equivalent to a group of [01]. A group of [0-2] is equivalent to a group of [012]. It is actually faster to use the latter non-range notation until the range size exceeds 3.

So you can rewrite, for example, your last pattern as:


(0[1-9]¦[12][0-9]¦3[01])

Jim

otem

4:15 pm on Apr 10, 2007 (gmt 0)

10+ Year Member



Thank you,

That was very useful and most helpful.

It helped me cut down on my length to a more managable size.