A range of numbers is a set of numbers that falls within a certain range or span. It is defined by the minimum and maximum values of the set. For example, the range of the numbers 0, 1, 2, 3, 4, … 255 is 0 to 255. The minimum value in this range is 0, and the maximum value is 255. In this article let’s understand how we can create a regex for month and how regex can be matched for a given month name.
Regex (short for regular expression) is a powerful tool used for searching and manipulating text. It is composed of a sequence of characters that define a search pattern. Regex can be used to find patterns in large amounts of text, validate user input, and manipulate strings. It is widely used in programming languages, text editors, and command line tools.
Structure of a range from 0 to 255
- Value should be a number
- Value should be in range from 0 to 255
- No special characters allowed
- No other characters allowed
Regex for checking if value is in range from 0 to 255
Regular Expression for month name-
/^([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/gm
Test string examples for the above regex-
Input String | Match Output |
---|---|
312 | does not match |
-210 | does not match |
134 | matches |
254 | matches |
10 | matches |
Here is a detailed explanation of the above regex-
/^([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/gm
^ asserts position at start of a line
1st Capturing Group ([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])
1st Alternative [0-9]
Match a single character present in the list below [0-9]
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
2nd Alternative [1-9][0-9]
Match a single character present in the list below [1-9]
1-9 matches a single character in the range between 1 (index 49) and 9 (index 57) (case sensitive)
Match a single character present in the list below [0-9]
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
3rd Alternative 1[0-9]{2}
1 matches the character 1 with index 4910 (3116 or 618) literally (case sensitive)
Match a single character present in the list below [0-9]
{2} matches the previous token exactly 2 times
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
4th Alternative 2[0-4][0-9]
2 matches the character 2 with index 5010 (3216 or 628) literally (case sensitive)
Match a single character present in the list below [0-4]
0-4 matches a single character in the range between 0 (index 48) and 4 (index 52) (case sensitive)
Match a single character present in the list below [0-9]
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
5th Alternative 25[0-5]
25 matches the characters 25 literally (case sensitive)
Match a single character present in the list below [0-5]
0-5 matches a single character in the range between 0 (index 48) and 5 (index 53) (case sensitive)
$ asserts position at the end of a line
Global pattern flags
g modifier: global. All matches (don't return after first match)
m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)
Hope this article was useful to check if the value is in range from 0 to 255 using regex. In this article, we delved into the concept of ranges in numerical sets and explored how regular expressions (regex) can be harnessed to verify values within specific ranges, with a focus on the range from 0 to 255. Additionally, we examined a practical example of using regex to validate and match month names. Regular expressions serve as versatile tools for text manipulation, pattern recognition, and validation across programming, text editing, and command line scenarios.