0

I have a set of IP adresses with special format, which I have to check if it matches the needed regex pattern. My pattern right now looks like this:

private static final String FIRST_PATTERN = "([0-9]{1,3}\\\\{2}?.[0-9]{1,3}\\\\{2}?.[0-9]{1,3}\\\\{2}?.[0-9]{1,3})";

This pattern allows me to check strict IP adresses and recognize the pattern, when IP adresses are static, for example: "65\\.33\\.42\\.12" or "112\\.76\\.39\\.104, 188\\.35\\.122\\.148".

I should, however, also be able to look for some non static IP's, like this:

"155\\.105\\.178\\.(8[1-9]|9[0-5])"

or this:

"93\\.23\\.75\\.(1(1[6-9]|2[0-6])), 113\\.202\\.167\\.(1(2[8-9]|[3-8][0-9]|9[0-2]))"

I have tried to do it in several ways, but it always gives "false", when try to match those IP's. I searched for this solution for a decent amount of time and I cannot find it and also cannot wrap my head around of how to do it myself. Is there anyone who can help me?

UPDATE Whole code snippet:

public class IPAdressValidator {

Pattern pattern;
Matcher matcher;

private static final String FIRST_PATTERN = "([0-9]{1,3}\\\\{2}?.[0-9]{1,3}\\\\{2}?.[0-9]{1,3}\\\\{2}?.[0-9]{1,3})";

public IPAdressValidator() {
    pattern = Pattern.compile(FIRST_PATTERN);
}

public CharSequence validate(String ip) {
    matcher = pattern.matcher(ip);
    boolean found = matcher.find();
    if (found) {
        for (int i = 0; i <= matcher.groupCount(); i++) {

            int groupStart = matcher.start(i);
            int groupEnd = matcher.end(i);

            return ip.subSequence(groupStart, groupEnd);
        }
    }
    return null;
}

}

and my Main:

public class Main {

public static void main(String[] args) {

    IPAdressValidator validator = new IPAdressValidator();

    String[] ips = 
            "53\\\\.22\\\\.14\\\\.43",
            "123\\\\.55\\\\.19\\\\.137",
            "93\\.152\\.199\\.1",
            "(93\\.199\\.(?:1(?:0[6-7]))\\.(?:[0-9]|[1-9][0-9]|1(?:[0-9][0-9])|2(?:[0-4][0-9]|5[0-5])))",
            "193\\\\.163\\\\.100\\\\.(8[1-9]|9[0-5])",
            "5\\\\.56\\\\.188\\\\.130, 188\\\\.194\\\\.180\\\\.138, 182\\\\.105\\\\.24\\\\.15",
            "188\\\\.56\\\\.147\\\\.193,41\\\\.64\\\\.202\\\\.19"

    };

    for (String ip : ips) {
        System.out.printf("%20s: %b%n", ip, validator.validate(ip));
    }

}

}

David
  • 1
  • 1
  • Please show full code snippet. The ``\\\\\{2}?.`` looks wrong. – Wiktor Stribiżew Feb 02 '20 at 20:58
  • Just updated it with code. It basically means, that the IP should contain exactly two slashes in between. Should I also add my main class code snippet? – David Feb 02 '20 at 21:04
  • Please add `String ip`, what does `ip` look like? – Wiktor Stribiżew Feb 02 '20 at 21:18
  • `String ip` is just a local variable, which I have declared as a parameter to my "validate" method. It is not declared anywhere else. – David Feb 02 '20 at 21:49
  • So, what is its text value? It would help to debug the code. – Wiktor Stribiżew Feb 02 '20 at 21:53
  • Sorry for late answer. It just returns every ip I have in my test array (its name is just "ips"). I have updated the code, where I have included everything. Is it more helpful? – David Feb 02 '20 at 23:42
  • Please confirm you are trying to validate IP regexps with a regex. Your `ips` are actually regular expressions. Here, see [what `ips` look like](https://ideone.com/Ezig7q). [Here](https://regex101.com/r/Hc0Kqx/1) is what the `FIRST_PATTERN` matches. It is not clear what you are really doing. – Wiktor Stribiżew Feb 02 '20 at 23:47
  • I am sorry for confusion. That is correct, some of my ip's in `ips` are regular expressions, last part of them. That is why I want a regex to accept regex (if that is possible). The reason is because those ip's are not static, sometimes the last part of ips change from those adresses, so I want my regex still be capable of recognizing them as numbers. Is it even realistic what I am trying to do? And thank you for links, those are helpful. – David Feb 03 '20 at 01:06
  • Here is regex to validate regex - but If you're trying to get it to still validate the IP address at the same time, that's where I tap out. https://stackoverflow.com/a/172316/12689629 – Zaelin Goodman Feb 03 '20 at 13:11

0 Answers0