如何在Objective-C中使用正则表达式验证IP地址? [英] How to validate an IP address with regular expression in Objective-C?
本文介绍了如何在Objective-C中使用正则表达式验证IP地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在Objective-C中验证IP地址?
How to validate an IP address in Objective-C?
推荐答案
这是使用现代inet_pton的类别,它将为有效的IPv4或IPv6字符串返回YES.
Here's a category using the modern inet_pton which will return YES for a valid IPv4 or IPv6 string.
#include <arpa/inet.h>
@implementation NSString (IPValidation)
- (BOOL)isValidIPAddress
{
const char *utf8 = [self UTF8String];
int success;
struct in_addr dst;
success = inet_pton(AF_INET, utf8, &dst);
if (success != 1) {
struct in6_addr dst6;
success = inet_pton(AF_INET6, utf8, &dst6);
}
return success == 1;
}
@end
这篇关于如何在Objective-C中使用正则表达式验证IP地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文