OC 정규 일치 핸드폰 번호, 메일박스 등

1299 단어
핸드폰 번호 일치
NSString *str = @"";
NSString *pattern = @"(13\\d|14[579]|15[^4\\D]|17[^49\\D]|18\\d)\\d{8}";
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern: pattern options: nil error: &error];
NSArray *match = [regex matchesInString: str options: NSMatchingCompleted range: NSMakeRange(0, [str length])];
if (match.count != 0) {
   // ...
}

메일박스 일치
NSString *str = @"";
NSString *pattern = @"\\w[-\\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\\.)+[A-Za-z]{2,14}";
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern: pattern options: nil error: &error];
NSArray *match = [regex matchesInString: str options: NSMatchingCompleted range: NSMakeRange(0, [str length])];
if (match.count != 0) {
    // ...
}

신분증 일치
NSString *str = @"";
NSString *pattern = @"\\d{17}[0-9Xx]|\\d{15}";
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern: pattern options: nil error: &error];
NSArray *match = [regex matchesInString: str options: NSMatchingCompleted range: NSMakeRange(0, [str length])];
if (match.count != 0) {
    // ...
}

좋은 웹페이지 즐겨찾기