Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1

TOPIC:

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

  • JoomGuy
  • JoomGuy's Avatar Topic Author
  • Offline
  • Moderator
  • Moderator
  • Joomla Enthusiast, Lover of Cooking
  • Posts: 1115
  • Thank you received: 195
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!!!

Please Log in or Create an account to join the conversation.

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

  • g1smd
  • g1smd's Avatar
  • Offline
  • Junior Member
  • Junior Member
  • RegEx fiend!
  • Posts: 31
  • Thank you received: 26
^([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.
The following user(s) said Thank You: JoomGuy

Please Log in or Create an account to join the conversation.

Last edit: by g1smd. Reason: Add note about PCRE and POSIX.
  • Page:
  • 1
Time to create page: 0.056 seconds

  I want to save the world...... let's cook a solution about it.
VeCrea (Forum)

Get Started