NSRegularExpression
The NSRegularExpression class is used to represent and apply regular expressions to Unicode strings.
Check IP Address
문자열이 IP주소인지 확인하는 함수 구현은 아래와 같다.
/// @brief IP주소를 확인할 수 있는 정규표현식.
#define IP_ADDRESS_REGEXP @"^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}$"
/// @brief IP주소인지 확인한다.
+ (Boolean) isIpAddress: (NSString*) checkString
{
// check parameter.
if (checkString == nil || [checkString length] == 0) {
return false;
}
// 두 번째 아이템이 IP주소 형식인지 확인해야 한다.
@autoreleasepool {
NSError * error = nil;
NSRegularExpression * ipAddressRegexp = [[NSRegularExpression regularExpressionWithPattern:IP_ADDRESS_REGEXP
options:NSRegularExpressionCaseInsensitive
error:&error] autorelease];
if (error == nil) {
int matchCount = [ipAddressRegexp numberOfMatchesInString:checkString
options:0
range:NSMakeRange(0, [checkString length])];
if (matchCount >= 1) {
return true;
}
}
}
return false;
}
Favorite site
References
-
NSRegularExpression_Class.pdf ↩