Solved

Java regular expression for the IP range matching problem

Posted on 2011-03-23
12
626 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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:
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…

919 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now