Extracting IP Address From a Given String Using Regular Expressions
Prerequisites:
Given a string str, the task is to extract all the IP addresses from the given string. Let us see how to extract IP addresses from a file.
Input:
str=”The IPV4 address of Port A is: 257.120.223.13. And the IPV6 address is:fffe:3465:efab:23fe:2235:6565:aaab:0001″
Output:
257.120.223.13
fffe:3465:efab:23fe:2235:6565:aaab:0001
Create a regex pattern to validate the number as written below:
- 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}”
Follow the below steps to implement the idea:
- Create a regex expression to extract all the IP Addresses from the string.
- Use Pattern class to compile the regex formed.
- Use the matcher function to find.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
string subject
= "The IPV4 address of Port A is :257.120.223.13 . "
"And the IPV6 address "
"is:fffe:3465:efab:23fe:2235:6565:aaab:0001" ;
regex pattern(
"(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})" );
cout << "Available IP Addresses in the Given String Are:"
<< endl;
for (regex_iterator<string::iterator> it(
subject.begin(), subject.end(), pattern);
it != regex_iterator<string::iterator>(); ++it) {
cout << it->str() << endl;
}
regex pattern1(
"((([0-9a-fA-F]){1,4})\\:){7}([0-9a-fA-F]){1,4}" );
for (regex_iterator<string::iterator> it(
subject.begin(), subject.end(), pattern1);
it != regex_iterator<string::iterator>(); ++it) {
cout << it->str() << endl;
}
return 0;
}
|
Java
import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class GFG {
public static void main(String[] args)
{
String str
= " The IPV4 address of Port A is :257.120.223.13 . And the IPV6 address is:fffe:3465:efab:23fe:2235:6565:aaab:0001" ;
System.out.println(
"Available IP Addresses in the Given String Are:" );
extractIP_Addresses(str);
}
static void extractIP_Addresses(String str)
{
String strPattern[] = {
"(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})" ,
"((([0-9a-fA-F]){1,4})\\:){7}([0-9a-fA-F]){1,4}"
};
for ( int i = 0 ; i < strPattern.length; i++) {
Pattern pattern
= Pattern.compile(strPattern[i]);
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}
}
|
Python3
import re
subject = ( "The IPV4 address of Port A is :257.120.223.13 . "
"And the IPV6 address "
"is:fffe:3465:efab:23fe:2235:6565:aaab:0001" )
pattern_ipv4 = r '\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b'
print ( "Available IP Addresses in the Given String Are:" )
ipv4_addresses = re.findall(pattern_ipv4, subject)
for address in ipv4_addresses:
print (address)
pattern_ipv6 = r '\b(?:[A-Fa-f0-9]{1,4}:){7}[A-Fa-f0-9]{1,4}\b'
ipv6_addresses = re.findall(pattern_ipv6, subject)
for address in ipv6_addresses:
print (address)
|
C#
using System;
using System.Text.RegularExpressions;
namespace GFG
{
class Program
{
static void Main( string [] args)
{
string str = " The IPV4 address of Port A is :257.120.223.13 . And the IPV6 address is:fffe:3465:efab:23fe:2235:6565:aaab:0001" ;
Console.WriteLine( "Available IP Addresses in the Given String Are:" );
ExtractIPAddresses(str);
}
static void ExtractIPAddresses( string str)
{
string [] strPattern = {
@"(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})" ,
@"(([0-9a-fA-F]){1,4})\:(([0-9a-fA-F]){1,4})\:(([0-9a-fA-F]){1,4})\:(([0-9a-fA-F]){1,4})\:(([0-9a-fA-F]){1,4})\:(([0-9a-fA-F]){1,4})\:(([0-9a-fA-F]){1,4})\:(([0-9a-fA-F]){1,4})"
};
for ( int i = 0; i < strPattern.Length; i++)
{
Regex pattern = new Regex(strPattern[i]);
MatchCollection matches = pattern.Matches(str);
foreach (Match match in matches)
{
Console.WriteLine(match.Value);
}
}
}
}
}
|
Javascript
const subject = "The IPV4 address of Port A is :257.120.223.13 . And the IPV6 address is:fffe:3465:efab:23fe:2235:6565:aaab:0001" ;
const pattern = /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/g;
console.log( "Available IP Addresses in the Given String Are:" );
let match;
while ((match = pattern.exec(subject)) !== null ) {
console.log(match[0]);
}
const pattern1 = /((([0-9a-fA-F]){1,4})\:){7}([0-9a-fA-F]){1,4}/g;
while ((match = pattern1.exec(subject)) !== null ) {
console.log(match[0]);
}
|
Output
Available IP Addresses in the Given String Are:
257.120.223.13
fffe:3465:efab:23fe:2235:6565:aaab:0001
Time Complexity: O(n)
Space Complexity: O(1)
Last Updated :
20 Sep, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...