Results 1 to 7 of 7

Thread: Regex help?

  1. #1
    l3ane is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Default Regex help?

    Well I'm writing this yet again because it signed me out when I went to post it, but anyhow, I am new to Java and am trying to use regex to extract an IP from a header but seems the more I read the more confused I'm getting. If someone could take a moment to post an example of how to extract a IP (127.0.0.1 for example) from a string and than print it so I could look it over and get a better understanding I would be very thankful. I've spent a fair amount of time reading various regex pages with no luck. I would really appreciate it. It's not the regex itself that confuses me it's actually implementing it to do this.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Let's see what you've tried so far, and please let us know how it doesn't work for you. Also, have you Googled "Java regular expression validating ip address"? Looks promising.
    Last edited by Fubarable; 01-18-2011 at 05:16 AM.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

  4. #4
    l3ane is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Let's see what you've tried so far, and please let us know how it doesn't work for you. Also, have you Googled "Java regular expression validating ip address"? Looks promising.
    I've tried a bunch of different thigns, I have also checked those sites. I'm still confused on how to actually use the regex for this purpose. I don't see why it's so hard to just post an example of how to take the 127.0.0.1 out of a string like "Hey this is my 127.0.0.1", I've spent alot of time reading various sites and still am confused.

    Also I checked out that site you posted last night, which just further confused me. I'm sure there's got to be a simpler example that doesn't have all kinds of extra code in it.
    Last edited by l3ane; 01-18-2011 at 05:32 AM.

  5. #5
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    here's an example. note, this will not check whether your IP is really in a valid Class.
    Java Code:
    public class RegexDemo2 {
        private static Pattern ex = Pattern.compile("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}");
        private static Matcher match;
        public static void main(String[] args){
            String str = "this is my ip 127.0.0.1 the end. but here is another 10.10.1.0 ";
            match = ex.matcher(str);
            while (match.find() ){
                 System.out.println( match.group() );
            }
        }
    }

  6. #6
    jomypgeorge is offline Member
    Join Date
    May 2010
    Posts
    90
    Rep Power
    0

    Default

    first you have to define what the format for a correct IP address
    for example 192.168.0.198, 198.234.89.0,.......

    i don't know the rule regarding it but i think it consists of four parts separated by "." sign. if the numbers are of three digits then we can make regex as follows

    "([\\d]){1,3}\\.([\\d]){1,3}\\.([\\d]){1,3}\\.([\\d]){1,3}"

    i think you can change it as actual rule regarding IP address.

  7. #7
    l3ane is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by JavaHater View Post
    here's an example. note, this will not check whether your IP is really in a valid Class.
    Java Code:
    public class RegexDemo2 {
        private static Pattern ex = Pattern.compile("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}");
        private static Matcher match;
        public static void main(String[] args){
            String str = "this is my ip 127.0.0.1 the end. but here is another 10.10.1.0 ";
            match = ex.matcher(str);
            while (match.find() ){
                 System.out.println( match.group() );
            }
        }
    }
    Brilliant! Thanks alot, really appreciate it. Clears that up.

Similar Threads

  1. Using Regex
    By ron87 in forum New To Java
    Replies: 4
    Last Post: 09-12-2010, 06:26 AM
  2. Regex problem
    By Nimyz in forum Advanced Java
    Replies: 4
    Last Post: 05-14-2010, 07:17 AM
  3. Please Help - Regex
    By BeeGee in forum Advanced Java
    Replies: 0
    Last Post: 04-28-2010, 05:28 PM
  4. Help with regex
    By SteroidalPsycho in forum New To Java
    Replies: 2
    Last Post: 03-29-2010, 12:40 AM
  5. Using regex
    By SteroidalPsycho in forum New To Java
    Replies: 0
    Last Post: 03-28-2010, 11:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •