判断输入的IP格式是否正确

1.使用正则表达式

//利用正则表达式判断IP的输入格式是否符合标准

Regex objRegex = new Regex(@"^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$");

if(!objRegex.IsMatch(txtSystemIp.Text))
{
       MessageBox.Show("IP格式不正确!请重新输入!","提示",MessageBoxButton.OK);

       return;
}

2.

IPAddress ip;
if(System.Net.IPAddress.TryParse(ipString,out ip))
{
    MessageBox.Show("IP格式正确!","提示",MessageBoxButton.OK);

   return;

}
else
{
   MessageBox.Show("IP格式不正确!","提示",MessageBoxButton.OK);

  return;

}
3.
一个IP地址通常是4个用点“.”隔开的8位二进制数字(0~255)组成,地址总长为32位。目前,IP地址共分5类,即:A,B,C,D,E。其中,具体的分布情况如下。

A类地址:从0.0.0.0到127.255.255.255(用于大型规模网络)

B类地址:从128.0.0.0到191.255.255.255(用于中型规模网络)

C类地址:从192.0.0.0到223.255.255.255(用于小型规模网络)

D类地址:从224.0.0.0到239.255.255.255(代表特殊类型的IP地址)

E类地址:从240.0.0.0到247.255.255.255(代表特殊类型的IP地址)

根据这些特点,利用条件语句判定所输入的IP地址是否符合要求。

 

在C语言中,验证IP地址和子网掩码的格式是否正确可以使用正则表达式库来匹配常见的IPv4和IPv6格式。这里是一个基本的示例,展示了如何使用`<regex>`库来检查IPv4和IPv6加上掩码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <regex.h> // 定义正则表达式模式 const char *ipv4_pattern = "^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"; const char *ipv6_pattern = "^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$"; const char *mask_pattern = "^(255|25[0-4]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(255|25[0-4]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(255|25[0-4]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(255|25[0-4]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"; int is_valid_ip_subnet(char *input) { regex_t ipv4_regex, ipv6_regex, mask_regex; int rc; // IPv4 validation rc = regcomp(&ipv4_regex, ipv4_pattern, REG_EXTENDED); if (rc == 0 && strncmp(input, "IPv4:", 5) == 0) { size_t match_len = strlen(input) - 5; if (regexec(&ipv4_regex, input + 5, 0, NULL, 0) == 0 && regfree(&ipv4_regex)) { return match_len; } regfree(&ipv4_regex); } // IPv6 validation rc = regcomp(&ipv6_regex, ipv6_pattern, REG_EXTENDED); if (rc == 0 && strncmp(input, "IPv6:", 5) == 0) { size_t match_len = strlen(input) - 5; if (regexec(&ipv6_regex, input + 5, 0, NULL, 0) == 0 && regfree(&ipv6_regex)) { return match_len; } regfree(&ipv6_regex); } // Mask validation rc = regcomp(&mask_regex, mask_pattern, REG_EXTENDED); if (rc == 0 && strncmp(input, "Mask:", 5) == 0) { size_t match_len = strlen(input) - 5; if (regexec(&mask_regex, input + 5, 0, NULL, 0) == 0 && regfree(&mask_regex)) { return match_len; } regfree(&mask_regex); } return -1; // 输入无效 } int main() { char input[] = "IPv4:192.168.1.1/24"; printf("Input: %s\n", input); int result = is_valid_ip_subnet(input); if (result >= 0) { printf("Valid IP subnet with length: %d\n", result); } else { printf("Invalid IP subnet.\n"); } return 0; } ``` 这个程序首先定义了正则表达式模式,然后在`is_valid_ip_subnet`函数中分别对IPv4、IPv6和子网掩码进行匹配。如果输入符合相应的模式,并且以特定前缀开始(如"IPv4:"、“IPv6:”或“Mask:”),它会返回匹配的长度。否则,函数返回-1表示输入无效。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值