题解 | #验证IP地址#
验证IP地址
https://www.nowcoder.com/practice/55fb3c68d08d46119f76ae2df7566880
using System; using System.Collections.Generic; using System.Text.RegularExpressions; class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * 验证IP地址 * @param IP string字符串 一个IP地址字符串 * @return string字符串 */ public string solve (string IP) { // write code here var ipv4 = @"^((2(5[0-5]|[0-4]\d))|[1]?\d{1,2})(\.((2(5[0-5]|[0-4]\d))|[1]?\d{1,2})){3}$"; var ipv6 = @"^(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$"; return Regex.IsMatch(IP, ipv4) ? "IPv4" : Regex.IsMatch(IP, ipv6) ? "IPv6" : "Neither"; } }