gpt-4.1-mini-2025-04-14· OpenAI
WildChat-4.8M (AI2)
write regular expression to match number from 0 to 255
- User
write regular expression to match number from 0 to 255
- Assistant
To match a number from 0 to 255 using a regular expression, you need to cover the following cases:
- 0 to 9
- 10 to 99
- 100 to 199
- 200 to 249
- 250 to 255
Here is a regex pattern that captures this:
^(25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)$Explanation:
25[0-5]matches 250 to 2552[0-4]\dmatches 200 to 2491\d{2}matches 100 to 199[1-9]?\dmatches 0 to 99 (including single digit numbers like 0-9 and two digit numbers 10-99)^and$anchor the regex to ensure the entire string matches this pattern
Examples:
- Matches:
0,9,10,99,100,199,249,255 - Does NOT match:
256,300,-1,01(leading zeros are not handled explicitly here)