Regexes in programs Check IP address
阿新 • • 發佈:2020-08-21
An IP address consists of four numbers from 0 to 255, inclusive, divided by the dots. For example, 127.0.0.1 is a valid IP address, but 256.0.0.1 and 127.0.1 are invalid IP addresses.
For a given string output "YES" if this string is a valid IP address, otherwise output "NO".
Sample Input 1:
127.0.0.1
Sample Output 1:
YES
Sample Input 2:
256.0.0.1
Sample Output 2:
NO
Sample Input 3:
127.0.1
Sample Output 3:
NO
Sample Input 4:
192.168.123.231
Sample Output 4:
YES
Sample Input 5:
-1.0.0.1
Sample Output 5:
NO
Sample Input 6:
127.0.0.1.
Sample Output 6:
NO
import java.util.*; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String ip = scanner.nextLine(); String regex = "(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)"; boolean isIP = ip.matches("(" + regex + "\\.){3}" + regex); System.out.println(isIP ? "YES" : "NO"); } }