• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Java Regex

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am new to Java Regex, i have a requirement in which i need to write java regex for validating ip address keeping in mind the below points.
Basically i need to validate the search key word, which a user enters.


1) IP-address notation dictates that is consists of 4 “dotted-decimals” – i.e. 4 “digit strings” that are separated by a “.” (dot).
Each dotted-decimal is between 1-3 digits long and value of a dotted-decimal number must be between 0-255 to be valid.
Example of valid IP-address notation: 0.0.0.0 or 1.2.3.4 or 23.34.45.56 or 255.255.255.255 (>256 is not valid)

2) Example of allowed inputs:

a) dotted-decimal: .1 or 1. (note: could be with or without leading/trailing “.” !)
b) dotted-decimals: 1.2 or .2.3 or .2.3. or .3.4 (note: could be with or without leading/trailing “.” !)
c) dotted-decimals: 1.2.3 or 1.2.3. or .2.3.4 (note: could be with or without leading/trailing “.” !)
d) dotted-decimals: 1.2.3.4 (note: could be with or without leading/trailing “.” !)


Need Regex pattern for the above.

Thanks in Advance.

Regards,

Girish

 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
These two articles will be of help- Lesson: Regular Expressions and Regular Expressions and the Java Programming Language

The second article is concise.
 
Girish Havaldar
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Mohamed,
since the conditions are very confusing, can you give me some hint on how to proceed.
 
Marshal
Posts: 74054
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

The conditions for a valid IP address are actually very simple, and you have already described them.
Not sure how you manage not greater than 255, however.
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would not use regex to evaluate an ip address. Well I am but only because there is no other standard way:

Split the ip on the "." You'll have to use "\\." because the argument is a regex pattern and the dot needs to be escaped.
Parse every segment to an int and check its value.

(Are you keeping IPv6 in mind?)
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Not sure how you manage not greater than 255, however.


group() method of Matcher to extract each octet, then Integer.parseInt to check that the octet is in the right range.
 
You had your fun. Now it's time to go to jail. Thanks for your help tiny ad.
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic