Solved

Java regular expression for the IP range matching problem

Posted on 2011-03-23
12
631 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I have been reconstructing a PHP-based application that has grown into a full blown interface system over the last ten years by a developer that has now gone into business for himself building websites. I am not incredibly fond of writing PHP code o…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to implement Singleton Design Pattern in Java.

791 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