Welcome, Guest
Username: Password: Remember me

TOPIC: NUMBER RANGES - No leading Zero

NUMBER RANGES - No leading Zero 25 Sep 2012 15:03 #3977

  • JoomGuy
  • JoomGuy's Avatar
  • Offline
  • Moderator
  • Joomla Enthusiast, Lover of Cooking
  • Posts: 1115
  • Thank you received: 195
  • Karma: 64
Range: 1-200 - suggested handler intrange1to200
^([1-9]|[1-9][0-9]|[1][0-9][0-9]|20[0-0])$
Range: 1-100 - suggested handler intrange1to100
^([1-9]|[1-9][0-9]|100)$
Need help with your Cook/Joomla Project? . PM me to find out what I can help with. NO time wasters please!!!
The administrator has disabled public write access.

Re: NUMBER RANGES - No leading Zero 09 Nov 2012 15:22 #5172

  • g1smd
  • g1smd's Avatar
  • Offline
  • Junior Member
  • RegEx fiend!
  • Posts: 31
  • Thank you received: 26
  • Karma: 6
^([1-9]|[1-9][0-9]|[1][0-9][0-9]|20[0-0])$
[1] simplifies to 1 - you only need a character group if there's more than one character.

[0-0] might crash the RegEx parser. This simplifies to '0'.

[0-9] can usually be expressed as \d if your RegEx is allowed in PCRE format, and this aids readability, a [x-y] group being used only when it is not the full 0 to 9 range. If you can only use POSIX, you'll have to stick to [0-9] notation.


Try:
^([1-9]|[1-9]\d|1\d\d|200)$
However, the part [1-9]|[1-9]\d is "1 to 9 OR 1 to 9 plus a digit"

"10 to 99" is just "1 to 9 plus another digit".

The expression can be compressed, i.e. "1 to 9 plus optional digit": [1-9]\d?


So, 1 to 200 is:
^([1-9]\d?|1\d\d|200)$
( 1 to 9 and 10 to 99 | 100 to 199 | 200 )

Often the problem that needs to be solved can be defined in a different way leading to a much simpler pattern. :)


Oh, and 1 to 100 is:
^([1-9]\d?|100)$
( 1 to 9 and 10 to 99 | 100 )
Online since 1996.
Last Edit: 09 Nov 2012 15:35 by g1smd. Reason: Add note about PCRE and POSIX.
The administrator has disabled public write access.
The following user(s) said Thank You: JoomGuy
Time to create page: 0.102 seconds

Get Started