Published on

# Regex for port range from 0 to 65535

A port has 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, ... 65535 is 0 to 65535. The minimum value in this range is 0, and the maximum value is 65535. 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 port range from 0 to 65535

• Value should be a number
• Value should be in range from 0 to 65535
• No special characters allowed
• No other characters allowed

# Regex for checking if port value is in range from 0 to 65535

Regular Expression for month name-

/^([1-9]|[1-9][0-9]{1,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$/gm  Test string examples for the above regex- Input StringMatch Output 9992929does not match -210does not match 6021matches 22matches 443matches Here is a detailed explanation of the above regex- /^([1-9]|[1-9][0-9]{1,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$/gm

^ asserts position at start of a line
1st Capturing Group ([1-9]|[1-9][0-9]{1,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])
1st Alternative [1-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)
2nd Alternative [1-9][0-9]{1,3}
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]
{1,3} matches the previous token between 1 and 3 times, as many times as possible, giving back as needed (greedy)
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
3rd Alternative [1-5][0-9]{4}
Match a single character present in the list below [1-5]
1-5 matches a single character in the range between 1 (index 49) and 5 (index 53) (case sensitive)
Match a single character present in the list below [0-9]
{4} matches the previous token exactly 4 times
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
4th Alternative 6[0-4][0-9]{3}
6 matches the character 6 with index 5410 (3616 or 668) 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]
{3} matches the previous token exactly 3 times
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
5th Alternative 65[0-4][0-9]{2}
65 matches the characters 65 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]
{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)
6th Alternative 655[0-2][0-9]
655 matches the characters 655 literally (case sensitive)
Match a single character present in the list below [0-2]
0-2 matches a single character in the range between 0 (index 48) and 2 (index 50) (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)
7th Alternative 6553[0-5]
6553 matches the characters 6553 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 port value is in range from 0 to 65535 using regex.