问题描述:

This question already has an answer here:

  • Validate IPv4 address in Java

    13 answers

网友答案:

Use a regular expression. In this case, you'll want (roughly):

Pattern pattern = new Pattern.compile("(\\d{1,3}\\.){4}");

which you can then use as follows:

boolean isMatch = pattern.matcher(ipString).matches;

This isn't extremely precise in terms of validating an IP address -- better answers are here. But it does answer your question as written: it excludes symbols except for a "." separating digits.

网友答案:

You should use regex.

    String ipString = "[email protected]";
    System.out.println(ipString.matches("^\\d+(\\.\\d+){3}$"));

matches will return true only if user inputs a sequence of digits and dots.

public boolean matches(String regex) : Tells whether or not this string matches the given regular expression.

网友答案:

I believe this is what you are looking for:

  • using the JDK pattern matching

  • using the JDK InetAddress

  • using Apache Commons Validator

相关阅读:
Top