Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: java ip regex

  1. #1
    Member
    Join Date
    Apr 2013
    Posts
    72
    Thanks
    6
    Thanked 2 Times in 2 Posts

    Default java ip regex

    hi i have this regex that finds ip and port ie.121.121.121.121:0000; in a string

    "\\d{1,3}(?:\\.\\d{1,3}){3}(?::\\d{1,5})?"

    the problem is if the port is missing this regex still finds just the ip which i dont want i want it to be all or nothing ip:port i tought removing the ? at the very end should achieve this but i still have the same problem.
    does anyone know how or direct me to the information i need to chainge this to only identifing ip and port.
    thanks.


  2. #2
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: java ip regex

    Yes, your problem is the question mark. In a regular expression, the "?" means "Zero or 1 times". In your case of not having the ":0000" on the end would qualify for "Zero times". You were correct to remove the "?" and that should have worked. Could you post a set of code that i could test and see the issue occurring? A workaround may be to put a "{1}". It seems to me like you have an issue that is not just in the regex string.
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  3. #3
    Member
    Join Date
    Apr 2013
    Posts
    72
    Thanks
    6
    Thanked 2 Times in 2 Posts

    Default Re: java ip regex

    thanks it was a problem elsewhere in code thanks

Similar Threads

  1. [SOLVED] java.util.regex.Matcher value lengths
    By PDubUK in forum Java SE APIs
    Replies: 1
    Last Post: August 15th, 2011, 08:14 AM
  2. [SOLVED] Java Regex Help
    By newbie in forum Java Theory & Questions
    Replies: 7
    Last Post: January 26th, 2011, 08:01 AM
  3. Using Regular Expression (regex) in Java Programming
    By lordelf007 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: May 14th, 2010, 10:29 AM
  4. Urgent need a java regex pattern
    By mallikarjun_sg in forum Java Theory & Questions
    Replies: 1
    Last Post: May 6th, 2010, 05:51 AM
  5. [SOLVED] Java Regular Expressions (regex) Greif
    By username9000 in forum Java SE APIs
    Replies: 4
    Last Post: June 11th, 2009, 05:53 PM