[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Java regular expression for the IP range matching problem

Posted on 2011-03-23
12
Medium Priority
?
656 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 1500 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses
Course of the Month17 days, 16 hours left to enroll

830 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