Each of the four numbers has a valid range of 0 to 255.The following bash script contains a bash function which returns true if it is passed a valid IP address and false otherwise.I've recently written about using bash arrays and bash regular expressions, so here's a more useful example of using them to test IP addresses for validity.To belabor the obvious: IP addresses are 32 bit values written as four numbers (the individual bytes of the IP address) separated by dots (periods). cat ipv4_validation_Full Match: 0.0.0.1 220.127.116.11 192.168.1.1 10.2 10.216.1.212 192.168.1.255 255.255.255.255 0.0.0.0 Partial Match (IP Extraction from line) 18.104.22.168.1 -22.214.171.124 sfds10.2kgfd da.255adfdsfds sfds10.2kgfd NO Match 1.1.1.01 3...3 127.1. [\d])) Notepad Replace String Option 1: Replaces the whole IP (NO Change): $ Notepad Replace String Option 2: Replaces the whole IP octect by octect (NO Change) $ .$ .$ .$ Notepad Replace String Option 3: Replaces the whole IP octect by octect (replace 3rd octect value with 0) $ .$ .0.$ NOTE: The above will match any valid IP including 255.255.255.255 for example and change it to 255.255.0.255 which is wrong and not very useful of course. I find myself many times writing an Advanced Function that takes as its parameters only IP Addresses. Error: "An invalid IP address was specified." At line:1 char:1 [ipaddress]"2126.96.36.199" ~~~~~~~~~~~~~~~~~~~~~~ Category Info : Invalid Argument: (:) , Runtime Exception Fully Qualified Error Id : Invalid Cast Parse Target Invocation Test-IPaddress -IPAddress "192.168.1.1" Address : 16885952 Address Family : Inter Network Scope Id : Is IPv6Multicast : False Is IPv6Link Local : False Is IPv6Site Local : False Is IPv6Teredo : False Is IPv4Mapped To IPv6 : False IPAddress To String : 192.168.1.1 PS C:\Windows\system32 Test-IPaddress -IPAddress "2188.8.131.52" Test-IPaddress : Cannot validate argument on parameter 'IPAddress'. A quick way I found for validating that an IP Address was passed is using the [IPAddress] Type Accelerator and the parameter option of [Validate Script()] if we look at the type accelerator it self if we pass a valid IPv4 or IPv6 Address we get an IPAddress object: [ipaddress]"192.168.1.1" Address : 16885952 Address Family : Inter Network Scope Id : Is IPv6Multicast : False Is IPv6Link Local : False Is IPv6Site Local : False Is IPv6Teredo : False Is IPv4Mapped To IPv6 : False IPAddress To String : 192.168.1.1 [ipaddress]"2184.108.40.206" Cannot convert value "2220.127.116.11" to type "System.
[0-7] ) ) | 0x0*[0-9a-f] # Hexadecimal notation, 0x0 - 0xffffffff | 0 [0-3]? )\b"; I adapted the regular expression taken from JGsoft Regex Buddy library to C language (regcomp/regexec) and I found out it works but there's a little problem in some OS like Linux. |1\d | 0x0*[0-9a-f] # Hexadecimal 0x0 - 0x FF (possible leading 0's) | 0 [1-3]? Putting the value of the subject IP address inside parenthesis and assigning it to itself thereby turns it into an array where each dot separated number is assigned to an array slot.Note that there's no need to test that the numbers are greater than or equal to zero because the regular expression test has already eliminated any thing that doesn't consist of only dots and digits.