0

I'm currently new to Java Programming so I'm taking on a few questions on HackerRank. Therefor, I encountered a question which asking me to validate IP address.

Write a class called MyRegex which will contain a string pattern. You need to write a regular expression and assign it to the pattern such that it can be used to validate an IP address. Use the following definition of an IP address:

IP address is a string in the form "A.B.C.D", where the value of A, B, C, and D may range from 0 to 255. Leading zeros are allowed. The length of A, B, C, or D can't be greater than 3.Some valid IP address:

000.12.12.034 121.234.12.12 23.45.12.56 Some invalid IP address:

000.12.234.23.23 666.666.23.23 .213.123.23.32 23.45.22.32. I.Am.not.an.ip In this problem you will be provided strings containing any combination of ASCII characters. You have to write a regular expression to find the valid IPs.

Just write the MyRegex class which contains a String pattern . The string should contain the correct regular expression.

(MyRegex class MUST NOT be public)

Sample Input

000.12.12.034 121.234.12.12 23.45.12.56 00.12.123.123123.123 122.23 Hello.IP

Sample Output

true true true false false false

I just got an answer from the internet which is

class myRegex {
    public String pattern="([1][\\d][\\d]|[0][0][0]|([0][0]|)[\\d]|([0]|)[\\d][\\d]|[2][0-4][\\d]|[2][5][0-5])."
            + "([1][\\d][\\d]|[0][0][0]|([0][0]|)[\\d]|([0]|)[\\d][\\d]|[2][0-4][\\d]|[2][5][0-5])."
            + "([1][\\d][\\d]|[0][0][0]|([0][0]|)[\\d]|([0]|)[\\d][\\d]|[2][0-4][\\d]|[2][5][0-5])."
            + "([1][\\d][\\d]|[0][0][0]|([0][0]|)[\\d]|([0]|)[\\d][\\d]|[2][0-4][\\d]|[2][5][0-5])";
    
}

I mainly don't understand the public pattern why is it so long and what do things in there do with the class. I hope someone can help me pull through this. Thanks so much!

HMN
  • 37
  • 4
  • 1
    Plug it in regex101.com and read its description/interpretation. Since in regex you cannot simply write `value < 256` one has to include all possible allowed cases. – PM 77-1 Dec 02 '22 at 19:50
  • 1
    Regex tip: `[0]` is identical to just `0` and `[0-9]` is identical to `\d`. Removing all those unnecessary square brackets will make your regex easier to read and therefore easier to debug. – Bohemian Dec 02 '22 at 20:45

1 Answers1

1

Try this regex

    /(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/

The pattern for a number between 001 and 255 is

    (25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)

for each octet, it checks:

  • if the number starts with 25x, where x is a number between 0 and 5.

  • if the number starts with 2yx, where y is a number between 0 and 5 and x is a number between 0 and 9.

  • if the number starts like 0xx or 1xx, and x are numbers between 0 and 9.

just use \. for the dots and repeat the pattern 4 times.

Iván Larios
  • 126
  • 4
  • Can you explain what “?” means in this situation? I still don’t get the definition of “?” and i can’t find it on the page i search for – HMN Dec 03 '22 at 07:30
  • 1
    The ? means that the previous token (a token is, for example [0-9]) is optional. This regex will allow you to validate IPs like 9.9.9.9 without the unnecessary 0s. If you want the 0 on the left to be mandatory, you can remove the ?s. – Iván Larios Dec 03 '22 at 11:45