-1

I have tried to make this example work in Java: Private IP Address Identifier in Regular Expression

I want to check if a IP is a private one, except 127.x.x.x

But I'm going to get crazy, What is wrong in this code?:

private static final Pattern patternPrivateNotLocal = Pattern.compile("(^10\\.)|(^172\\.1[6-9]\\.)|(^172\\.2[0-9]\\.)|(^172\\.3[0-1]\\.)|(^1‌​92\\.168\\.)");
public static boolean isPrivateAndNotLocalIP(String ip)
{
    return  patternPrivateNotLocal.matcher(ip).matches();
}

The method is returning me false when ip = "192.168.122.1"

Community
  • 1
  • 1

1 Answers1

2

I copied and paste your code to retry it, but to my suprise it didn't work for me, however, I retyped everything and it started working.

can't say for sure but it must be encoding issues.

just try to "retype" everything and change matches() to find()

if you are going to use matches method, you have to match the full text for example

(^10\\.) should be changed to (^10\\..*)

where as find method finds the first token matched and return true if any available

nafas
  • 5,283
  • 3
  • 29
  • 57