Solved

Java regular expression for the IP range matching problem

Posted on 2011-03-23
12
629 Views
Last Modified: 2012-05-11
I need a regular Java regular expression for the below problem. I have some list of ip address ranges as patterns in a text file. How extract the matched ip addresses from the given bulk string data.

For example the text file contains:
60.\.19\.164\.[0-9]{1,3}
61\.19\.165\.[0-9]{1,3}
61\.19\.166\.[0-9]{1,3}
61\.19\.167\.[0-9]{1,3}
62\.19\.168\.[0-9]{1,3}

The given String:
Experts are ready to help solve your problem. The quality of your question will directly influence the speed and accuracy of the answers you receive.The given ip address is 61.19.167.123 and also it may be 61.19.167.12.
This is the next line of text and which contains the following ip address 161.19.167.19 and this should not match.
This is the third line and which contains 61.78 4.9 this also should not match. But this should match.60.19.164.10

Note:
Here 161.19.167.19 contains 61.19.167.19 but its not valid ip, so it should not match.

Thanks in advance.
0
Comment
Question by:soundar_vk
  • 5
  • 3
  • 2
  • +1
12 Comments
 
LVL 41

Expert Comment

by:HonorGod
ID: 35202305
This article is for you!

Checking for valid IP addresses using JavaScript Regular Expressions
http://www.experts-exchange.com/A_1074.html
0
 

Author Comment

by:soundar_vk
ID: 35202478
Thanks for the reply.But thats not helpful for me (may be i didnt know how to derive things from that article).  But basically i need something like bigger regular expression framed from the file content and matching the given text which will give the list of matched ip addresses.
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 35202543
Ah.  Sorry for misunderstanding.

So, you want to be able to specify (or read in) a RegExp that should be used to verify/validate specific IP address ranges?
0
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
LVL 47

Expert Comment

by:for_yan
ID: 35202612




This works for me but not everything is in RegEx
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MyRegEx {

    public MyRegEx(){

            Pattern p = Pattern.compile("60.19.164.[0-9]{1,3}|61.19.165.[0-9]{1,3}|61.19.166.[0-9]{1,3}|[^0-9]61.19.167.[0-9]{1,3}|62.19.168.[0-9]{1,3}");

   String s =
"Experts are ready to help solve your problem. The quality of your question will directly influence the speed and" +
        " accuracy of the answers you receive.The given ip address is 61.19.167.123 and also it may be 61.19.167.12.  " +
"This is the next line of text and which contains the following ip address 161.19.167.19 and this should not match. " +
"This is the third line and which contains 61.78 4.9 this also should not match. But this should match.60.19.164.10 ";





    //    String s = "This is the third line and which contains 61.78 4.9 this also should not match. But this should match.60.19.164.10";
             Matcher m = p.matcher(s);
        while(m.find()){
            String s1 = s.substring(m.start(),m.end());
            if(!Character.isDigit(s1.charAt(0)))s1 = s1.substring(1);
            System.out.println(s1);
        }


    }

public static void main(String [] args){
    new MyRegEx();

}
}

Open in new window


Output:
61.19.167.123
61.19.167.12
60.19.164.10

Open in new window

0
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 35202677

This is with correction related to dots:

import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class MyRegEx {

    public MyRegEx(){


        Pattern p = Pattern.compile("60\\.19\\.164\\.[0-9]{1,3}|61\\.19\\.165\\.[0-9]{1,3}|61\\.19\\.166\\.[0-9]{1,3}|[^0-9]61\\.19\\.167\\.[0-9]{1,3}|62\\.19\\.168\\.[0-9]{1,3}");

   String s =
"Experts are ready to help solve your problem. The quality of your question will directly influence the speed and" +
        " accuracy of the answers you receive.The given ip address is 61.19.167.123 and also it may be 61.19.167.12.  " +
"This is the next line of text and which contains the following ip address 161.19.167.19 and this should not match. " +
"This is the third line and which contains 61.78 4.9 this also should not match. But this should match.60.19.164.10 ";





    //    String s = "This is the third line and which contains 61.78 4.9 this also should not match. But this should match.60.19.164.10";
             Matcher m = p.matcher(s);
        while(m.find()){
            String s1 = s.substring(m.start(),m.end());
            if(!Character.isDigit(s1.charAt(0)))s1 = s1.substring(1);
            System.out.println(s1);
        }


    }

public static void main(String [] args){
    new MyRegEx();

}
}

Open in new window

0
 

Author Closing Comment

by:soundar_vk
ID: 35202719
This helped me. Thanks for_yan.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35202916
You are welcome.

@HonorGod

I tried to combine your great article with the Java code and to check if matched strings satsfy IP pattern in general.
However it does not pass these strings - result is no matches.
Perhaps I didn't pick up your regex for general IP correctly.
I'd appreciate if you could spot my error in regS:

import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class MyRegEx {

    public MyRegEx(){


        Pattern p = Pattern.compile("60\\.19\\.164\\.[0-9]{1,3}|61\\.19\\.165\\.[0-9]{1,3}|61\\.19\\.166\\.[0-9]{1,3}|[^0-9]61\\.19\\.167\\.[0-9]{1,3}|62\\.19\\.168\\.[0-9]{1,3}");

      //Pattern p1 = Pattern.compile("^(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])$");

   String s =
"Experts are ready to help solve your problem. The quality of your question will directly influence the speed and" +
        " accuracy of the answers you receive.The given ip address is 61.19.167.123 and also it may be 61.19.167.12.  " +
"This is the next line of text and which contains the following ip address 161.19.167.19 and this should not match. " +
"This is the third line and which contains 61.78 4.9 this also should not match. But this should match.60.19.164.10 ";


           String regS =    "(?:/^(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])$/\\.){3}/^(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])$/";


    //    String s = "This is the third line and which contains 61.78 4.9 this also should not match. But this should match.60.19.164.10";
             Matcher m = p.matcher(s);
        while(m.find()){
            String s1 = s.substring(m.start(),m.end());
            if(!Character.isDigit(s1.charAt(0)))s1 = s1.substring(1);
            if(Pattern.matches(regS,s1))System.out.println(s1);
        }


    }

public static void main(String [] args){
    new MyRegEx();

}
}

Open in new window


0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35202974
>>
For example the text file contains:
60.\.19\.164\.[0-9]{1,3}
>>

Does that first one contain a typo?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35203010
>> I have some list of ip address ranges as patterns in a text file.

Then the correct way to do it is to test each pattern, not to concatenate them
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35203014
No I believe I misinterpreted your RegEx
which I picked up from your article in this line

 String regS =    "(?:/^(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])$/\\.){3}/^(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])$/";

When I added this regex and the last check in the program:

if(Pattern.matches(regS,s1))System.out.println(s1);

it does not produce outputr any more - this last statement does not produce a match.
If I remove this check of regS to s1, then  it prints three IPs as expected:

61.19.167.123
61.19.167.12
60.19.164.10

But for some reason they do not survive this check.
Don't know there may be possibility that the issue is with my java, not with regS.






0
 
LVL 41

Expert Comment

by:HonorGod
ID: 35203271
Note that the RegExp that you got from the article is expecting (and requiring) the IP address to be by itself in the string.

It includes ^ (beginning of line/string) and $ (end of line/string)
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35203440

OK. This finally combines matching required IP ranges
together with checking overall IP pattern in the spirit
of the above article  

import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class MyRegEx {

    public MyRegEx(){


        Pattern p = Pattern.compile("60\\.19\\.164\\.[0-9]{1,3}|61\\.19\\.165\\.[0-9]{1,3}|61\\.19\\.166\\.[0-9]{1,3}|[^0-9]61\\.19\\.167\\.[0-9]{1,3}|62\\.19\\.168\\.[0-9]{1,3}");

      //Pattern p1 = Pattern.compile("^(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])$");

   String s =
"Experts are ready to help solve your problem. The quality of your question will directly influence the speed and" +
        " accuracy of the answers you receive.The given ip address is 61.19.167.123 and also it may be 61.19.167.12.  " +
"This is the next line of text and which contains the following ip address 161.19.167.19 and this should not match. " +
"This is the third line and which contains 61.78 4.9 this also should not match. But this should match.60.19.164.10 ";


           //String regS =    "(?:/(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])/\\.){3}/(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])/";
         String regS =    "((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])\\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])";
                              /*
                   String regS1 =    "25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9]\\.";
               String regS2 = "((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])\\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])";
      System.out.println(  Pattern.matches(regS2,"101.102.103.104"));
        */

    //    String s = "This is the third line and which contains 61.78 4.9 this also should not match. But this should match.60.19.164.10";
             Matcher m = p.matcher(s);
        while(m.find()){
            String s1 = s.substring(m.start(),m.end());
            if(!Character.isDigit(s1.charAt(0)))s1 = s1.substring(1);
            if(Pattern.matches(regS,s1))System.out.println(s1);
        }


    }

public static void main(String [] args){
    new MyRegEx();

}
}

Open in new window



61.19.167.123
61.19.167.12
60.19.164.10

Open in new window

0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question