Wednesday, February 5, 2020

Java - Regex to check for valid IP's

Regular expressions

regular expression defines a search pattern for strings. The abbreviation for regular expression is regex. The search pattern can be anything from a simple character, a fixed string or a complex expression containing special characters describing the pattern. The pattern defined by the regex may match one or several times or not at all for a given string.
Regular expressions can be used to search, edit and manipulate text.

Valid IP Address

The basic format of ip addresses format policy is:
  • It must start with a number from 0 – 255.
  • It must be followed a dot
  • This pattern has to repeat for 4 times (eliminating the last dot…)
This is the regex we will use:

"^(([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.){3}" +
"([01]?\\d\\d?|2[0-4]\\d|25[0-5])$"

This will throw the following output:
  • 000.12.12.034 -> valid
    
  • 121.234.12.12 -> valid
    
  • 23.45.12.56 -> valid
    
  • 00.12.123.123123.123 -> invalid
    
  • 122.23 -> invalid
    
  • Hello.IP -> invalid

Thursday, September 26, 2019

Java: Smallest Multiple

This post is made gathering data from other nice sites, but just wanted to have it in a common place.

This is the solution for Project Euler, question #5: Smallest Number.