How to validate an IP address using ReGex | C ++ program to validate IP address using Regex ; Function for Validating IP ; Regex expression for validating IPv4 ; Regex expression for validating IPv6 ; Checking if it is a valid IPv4 addresses ; Checking if it is a valid IPv6 addresses ; Return Invalid ; Driver Code ; IP addresses to validate
#include <bits/stdc++.h> NEW_LINE using namespace std ; string Validate_It ( string IP ) { regex ipv4 ( " ( ( [ 0-9 ] ▁ [ 1-9 ] [ 0-9 ] ▁ 1[0-9 ] [ 0-9 ] ▁ 2[0-4 ] [ 0-9 ] ▁ 25[0-5 ] ) \\ . ) { 3 } ( [0-9 ] ▁ [ 1-9 ] [ 0-9 ] ▁ 1[0-9 ] [ 0-9 ] ▁ 2[0-4 ] [ 0-9 ] ▁ 25[0-5 ] ) " ) ; regex ipv6 ( " ( ( ( [0-9a - fA - F ] ) { 1,4 } ) \\ : ) { 7 } ( [0-9a - fA - F ] ) { 1,4 } " ) ; if ( regex_match ( IP , ipv4 ) ) return " Valid ▁ IPv4" ; else if ( regex_match ( IP , ipv6 ) ) return " Valid ▁ IPv6" ; return " Invalid ▁ IP " ; } int main ( ) { string IP = "203.120.223.13" ; cout << Validate_It ( IP ) << endl ; IP = " fffe : 3465 : efab : 23fe : 2235:6565 : aaab : 0001" ; cout << Validate_It ( IP ) << endl ; IP = "2F33:12a0:3Ea0:0302" ; cout << Validate_It ( IP ) << endl ; return 0 ; }