Contemplating an entry about Regular Expressions
January 29, 2008
Posted in: Javascript, Programming, Regular Express
Tags: data type, INET, IP, Javascript, octet, postgres, Programming, regular expression
I’m sitting here getting ready to write up a quick and dirty little regular expression for checking INET values, when it got me thinking. I’ve somehow turned my blog into a more technical blog then just some ramblings of mine. My technical posts out number general (or non-geeky) blog posts.
It’s not such a bad thing, in my mind, but that’s why I have another site. It’s not currently working due to the data loss suffered earlier in the month, and I haven’t gotten around to getting it back up and running yet. So what shall I do? I suppose I could just cross post them between the two and try to focus more on the generality of this blog.
It’ll probably straighten itself out in long run (once the other site is up and running), but until then - I guess you’ll be stuck with geeky entries here.
So back to my original post. So let’s say you are using postgres, and you are also using the INET data type. You then have a form to update these values. How do you keep from getting errors or not values and having irritated users?
Well it just so happens I have that exact problem, well I did have that problem. I’ll show you the code first and then try to explain it (if I can remember…) how it works.
var inet_ip = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.
(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.
(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.
(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])
((\/)([0-9]|[1-2][0-9]|30|31|32))?$/;
excuse me what? Did you hurt yourself trying to figure it out - I know I did when I was first trying to use regular expressions. But I think this will make sense easy enough.
So first we have our variable declaration:
var inet_ip
I’m just telling javascript that the variable inet_ip equals that huge nasty thing after it.
To make this somewhat easier let’s break up the string into sections, like oh I don’t know how about into octets. Octets you ask, why yes octets - they are the 4 sections of an ip address such as 192.168.0.1 but an INET value also have the range at the end, like /22.
It’s not so bad, really…watch.
The first section of the regex is ^ which means the start of the string - the starting point of you expression.
The next piece is for the first octet. We know that an IP address needs to be a number of up to three character in each octet. So let’s search for it.
(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])
\d{1,2} = digits 1 or 2 characters long (eg. 0 through 99)
| = or (that’s a pipe character usually below the backspace key)
1\d\d = basically 100 -199
| = or
2[0-4]\d = 200 - 249 (the [0-4] means numbers 0 through 4)
| = or
25[0-5] = 250 - 255
I know you’re asking yourself can an IP address actually start with a 0? No it can’t and you can modify this to suit your needs I should make it only except 10 or 192. Alright I went ahead and fixed it. It now would read like this:
(10|192)
Which means match the literal number 10 or 192. Now you could modify this according to your own needs, and I think I’ve given enough away to do so.
Now the expression does the same thing for then rest of the octets so we can skip over those. But I should point out that in between each of those we have a \. this means that there will be a period in between each octet. So to be safe I escape the period with a \ so it matches a literal period.
The final part is specifically for INET values. Now I have only a certain number of ranges, which I believe is part of the INET data type. But this is how it works.
((\/)([0-9]|[1-2][0-9]|30|31|32))?
The first piece is (\/)? This isn’t a capital V, this is an escaped / because when you write out a INET you would do like so 10.10.10.10/32 so I need to match that /. The next bit should be easy to read now.
[0-9]|[1-2][0-9]|30|31|32)
I need to match digits, 0-9 or 10-29 or 30 or 31 or 32. The last little bit gives a little leeway to the person using the form. The whole piece at the end ((\/)([0-9]|[1-2][0-9]|30|31|32))? is wrapped in ()? that question mark bascially says if it’s there. So matching it would mean match 10.10.10.10 or 10.10.10.10/32 but it would not match 10.10.10.10/ or 10.10.10.1032.
So the $, this is the opposite of ^, it means then end of the string.
I know that was a pretty quick run through - but I think I covered most of what is needed to understand how to check INET values with a form. There’s a lot of great resources out there too.
Try this cheat sheet
Try this site (very useful too)

Posted in 

content rss


January 29th, 2008 at 8:10 pm
Grin - keep them coming.

Not that I understand any of what you are saying, but hey, I might pick something useful up on the long run, no?
January 29th, 2008 at 11:43 pm
I tell you what, ask a question and I’ll try to answer it…but I hope I’m posting useful information to someone..LOL