In this tutorial you will learn how to match an IP address in Regular Expressions step by step in simple way. You will also learn how to find and validate an ip address in regex.
To write regex for an ip address the basic thing is to have knowledge about how to write regex code for numbers. Regex does not understand numbers as numbers but only string characters so any range given in terms of numbers or mathematical operations are not going to work. As will all the cases to write regex for ip first we have to analyze the problem and visualize complexity in this case so that an efficient and effective regex code is written for matching internet protocol address.
If you want to learn Regex with plenty of examples and logic, I will strongly suggest you to see this simple and to the point Regex Course with practical examples and step by step approach with exercises. This video course teaches you the Logic and Philosophy of Regular Expressions from the Beginners level to advanced level.
You can study more about regex for numbers and number ranges
At first the format of an ip address seems intimidating as it contains four number ranges and three periods as seperators. But after having a close look at it, one comes to know that infact the four numbers are of the same nature, of same range from 0-255 hence if we write regex for one number, we can simply copy and paste it three times and that will be the regex code for ip address.
First thing is to look at an ip address. As shown in the figure above an ip address is in the form of
X . X . X . Xwhere X can be any number from 0 to 255, and this value can not be more than 255. However for 0 different forms of presentation can be used like 0, 00, 000 moreover 1 can be represented as 001 and so on. We have to keep that in mind that 001 or 009 is also a possibility and code must match this format also in addition to 1 and 9. For this complex structure of ip address we will use divide and conquer technique.
If we can write regular expression for X, then we can easily write regex for whole ip address as this x is repeating four times with a dot as seperator.
When you are writing regex for a number, you have to keep in mind that regex does not know numbers but takes them as string characters. Hence the solution [0-255] is not going to work. Can you guess what this regex is going to match [0-255]? check it and the results will surely surprise you. In ip address the maximum number in our range is 255 which is three characters long. Minimum number is 0 which is one character long. To write a regex for matching this range 0-255 we will breakdown this range into smaller ranges which can be easily managed for writing regex.
So the breakdown is
1. 250 -255
After the breakdown now we have to write regex for three ranges instead of a single range.
Starting from the first component or range 250-255 the regex code for first range 250-255 is / 25[0-5] / this code will match numbers from 250 to 255. Here there are total of six numbers 250, 251, 252, 253, 254, 255. Now in these numbers 25 is common so we have taken 25 with the range 0-5.
For the second component or range 200 to 249. Only 2 is common at first place so we will write 2, after that at second place the minimum number is 0 and maximum is 4 hence the range will be [0-4] and at third place the minimum number is 0 and maximum is 9 so the range will be [0-9] for third place. Complete regex for this range will be then / 2[0-4][0-9] / it will match any number from 200 to 249.
Similarly for matching 0-199 the code will be / ?[0-9][0-9]?/ This regex will also match numbers like 0,00,000,001,009,011, 010 and so on as the question mark makes the group or literal an optional entity.
Regex for X will be ( 25[0-5] | 2[0-4][0-9] | ?[0-9][0-9]? )
After writing regex for X, now we can repeat it for writing the complete code for ip address. Here is the example
/ ( 25[0-5]|2[0-4][0-9]|?[0-9][0-9]? ) \. ( 25[0-5]|2[0-4][0-9]|?[0-9][0-9]? ) \.
( 25[0-5]|2[0-4][0-9]|?[0-9][0-9]? ) \. ( 25[0-5]|2[0-4][0-9]|?[0-9][0-9]? ) /