Author Topic: Regex help!  (Read 955 times)

Offline Zeggy

  • Global Moderator
  • Level 35
  • *****
  • Posts: 1,187
  • Reputation: +13/-4
    • View Profile
Regex help!
« on: May 05, 2007, 08:25:32 AM »
I need help with the regular expression in one of my .htaccess files...

This is the rewrite rule I currently have:
Code: [Select]
RewriteRule ^([^/\.]+)/?$ index.php?game=$1 [L]
It is suppose to match a url like http://www.domain.com/number/ and redirect to index.php?game=number

Unfortunately, it only detects numbers with one character, so /1/ to /9/ works, but /10/ and higher doesn't work...

How can I get this to match all numbers, regardless of the number of digits? I tried using \d, but that doesn't seem to work at all. I've also tried using "([1-9]+[0-9]?[0-9]?)" but none of these seem to work the way I want it :O

Thanks!
« Last Edit: May 05, 2007, 08:30:46 AM by Zeggy »

Offline Broda

  • Level 13
  • *
  • Posts: 97
  • Reputation: +2/-0
    • View Profile
    • Nightfall Games
Re: Regex help!
« Reply #1 on: May 05, 2007, 09:26:28 AM »
[1-9][0-9]?

^ should match a number followed 0 or more other numbers
[1-9][0-9]?[0-9]? <- doesn't make sense only need 1 [0-9]?

Offline beam

  • Level 15
  • *
  • Posts: 132
  • Reputation: +2/-0
  • Dance Commander
    • View Profile
Re: Regex help!
« Reply #2 on: May 05, 2007, 02:20:14 PM »
To match a number, this is what you should use:

Code: [Select]
[1-9][0-9]*

That way, it eliminates leading zeroes, and the rule can be  read as "any number from 1 to 9, followed by any amount of numbers from 0 to 9."

Or you could do this for numbers from 0 to 999 (which is what I thought you were trying to get):

Code: [Select]
[1-9][0-9]{0,2}
« Last Edit: May 05, 2007, 02:22:13 PM by beam »

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal