Reg expression to accept values between 1 and 48? [closed]

You could try this regular expression: ^0*(?:[1-9]|[1-3][0-9]|4[0-8])$

However, I’d advice using another mechanism for numeric validation if possible. Regular expressions are meant to match string patterns, as already pointed out in the comments.

Here’s an explanation of the regex part by part:

  • ^: Beginning of string
  • 0*: Match any leading 0’s
  • (?: : Grouping the ORs (non-capturing group)
    • [1-9]: Match 1 to 9
  • |: OR
    • [1-3][0-9]: Matches 10 to 39
  • |: OR
    • 4[0-8]: Matches 40 to 48
  • ) : Ends grouping the ORs
  • $: End of string

See it working in this RegExr.

Some resources you might find useful to assist desiging/testing them yourself in the future:

Leave a Comment