Regex: Number Between 0 and 50

# Regex for validating a number between 0 and 50

To validate a number between 0 and 50 using a regular expression, you can use the following pattern:

### A possible regex
“`
^(?:[0-9]|[1-4][0-9]|50)$
“`

### Regex Breakdown
“`
^ # Matches the start of the string
(?: # Starts a non-capturing group
[0-9] # Matches any single digit from 0 to 9
| # OR
[1-4][0-9] # Matches any two-digit number starting with 1 to 4
| # OR
50 # Matches the number 50
) # Ends the non-capturing group
$ # Matches the end of the string
“`

Examples of strings that should match this regular expression are:
– `0`
– `25`
– `50`

Examples of strings that should not match this regular expression are:
– `-1`
– `51`
– `100`

### Final notes
This regular expression will match any number between 0 and 50, inclusive. If you want to allow leading zeros (e.g., `01`, `02`, etc.), you can modify the pattern accordingly.

Read more here: Source link