题解 | #合法IP# 强行正则匹配
合法IP
https://www.nowcoder.com/practice/995b8a548827494699dc38c3e2a54ee9
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { // 注意 while 处理多个 case String s = sc.nextLine(); String out = "YES"; if(!s.matches("([0]|[1-9]+[0-9]*)\\.([0]|[1-9]+[0-9]*)\\.([0]|[1-9]+[0-9]*)\\.([0]|[1-9]+[0-9]*)")){ out="NO"; }else{ String[] ip = s.split("\\."); for(int i=0;i<4;i++){ if(Integer.parseInt(ip[i]) < 0 || Integer.parseInt(ip[i]) > 255 ){ out="NO"; } } } System.out.print(out); } } }