Don't forget that this is not sufficient by itself to determine if a string is a valid ipv4 numeric ip. You will have to split the string at '.' and test if each octet is <= 255.
I suppose life gets even more difficult if you also want to determine if it is a valid external IP, for example not loopback or private lan. I have some code somewhere for these tests. If you have any interest, I will try to dig it out.