
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Validate IPv6 address using ReGex patterns in C++
Given an IP Address, the task is to validate this IP address and check whether it is IPv6 or not with the help of ReGex(Regular Expression). If the IP Address is valid then print “IPv6 Address” otherwise print “Not”.
A valid IPv4 address is an IP in the form "XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX" where each Xi digit is a hexadecimal digit. For example,
Input-1 −
IP= “3001:0da8:82a3:0:0:8B2E:0270:7224”
Output −
“Not”
Explanation − This is not a valid IPv6 address, return “Not”.
Input-2 −
IP= “2001:0db8:85a3:0000:0000:8a2e:0370:7334”
Output −
“IPv6”
Explanation − This is a valid IPv6 Address, return “IPv6”.
Approach to solve this problem
To check whether the given IP address is IPv6 or not, we use ReGex. A ReGex is an expression that contains a sequence of characters that define a specific pattern. These patterns can be used in algorithms to match the pattern in a string. It is also widely used for Input Validation.
Range Specification − We can specify the characters to make the patterns in the simplest way. To specify the range by using characters, we can use ‘[ ]’ brackets.
Specifying Characters − The above expression indicates an opening bracket and a digit in the range a to z , ‘A’ to ‘Z’ and ‘0’ to ‘9’ as a regex.
[a-z], [A-Z] and [0-9].
Repeated Patterns − An expression modifier can be “+” that suggests matching the occurrence of a pattern one or more times or it can be “*” that suggests matching the occurrence of a pattern zero or more times.
The expression [a-z]* will match a blank string.
If you want to specify a group of characters to match one or more times, then you can use the parentheses as follows −
[Abc]+
Following is the approach to solve this problem
Take Input a string specifying a IP address.
A string function validIPAddress(string IP) takes IP address as input and checks whether the input string is valid or not. If it is valid then return “IPv6” otherwise return “Not an IP Address”.
Creating a regex pattern for the IPv6 address. Since an IPv6 address contains 8 fields in which each field contains the value digits represented as hexadecimal. An IPv6 address looks like XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX separated by ‘colon’.
A valid IPv6 address might be in the range ([0-9a-fA-F]){1,4})\:){7}([0-9a-fA-F]){1,4}) in which the first digit will be in the ranges from 0-9, second is hexadecimal alphanumeric digit.
Similarly, for the second field first character would be in the range of 0-9a-fA-F thus the regex pattern would be ‘[0-9a-fA-F]’
Example
#include<bits/stdc++.h> using namespace std; string validIPAddress(string IP) { regex ipv6("((([0-9a-fA-F]){1,4})\:){7}([0-9a-fA-F]){1,4}"); if(regex_match(IP, ipv6)) return "IPv6"; else return "Not"; } int main(){ string IP= “3001:0da8:82a3:0:0:8B2E:0270:7224”; string ans= validIPAddress(IP); cout<<ans<<endl; return 0; }
Output
Running the above code will generate the output as,
Not
Since the input IP Address is not a valid IP address, we will return “Not”.
- Related Articles
- Validate IPv4 address using ReGex patterns in C++
- Validate IP Address in C#
- Validate IP Address in C++
- Transition from IPv4 to IPv6 address
- How to validate an email address in C#?
- How to validate email address using RegExp in JavaScript?
- Validate Email address in Java
- Validate IP Address in Python
- C Program to validate an IP address
- How to validate the IP address using PowerShell?
- How to validate Email Address in Android on EditText using Kotlin?
- How to validate an email address using Java regular expressions.
- How to Match patterns and strings using the RegEx module in Python
- How to validate URL address in JavaScript?
- How to validate email address in JavaScript?
