//组装一个字符串,把里面的网址解析出来
NSString *urlString = @"sfdshttp://www.baidu.com";
NSError *error;
//http+:[^\s]* 这是检测网址的正则表达式
NSRegularExpression *regex = [NSRegularExpressionregularExpressionWithPattern:@"http+:[^\s]*"options:0error:&error];
if (regex != nil) {
NSTextCheckingResult *firstMatch = [regex firstMatchInString:urlString options:0range:NSMakeRange(0, [urlString length])];
if (firstMatch) {
NSRange resultRange = [firstMatch rangeAtIndex:0];
//从urlString中截取数据
NSString *result = [urlString substringWithRange:resultRange];
NSLog(@"%@",result);
}
}
NSString *stringC = @"weiboyuan@163.com";
//匹配输入的联系方式是否为QQ号码或者电子邮箱
NSString *patternQQ = @"^[1-9](\d){4,9}$";
NSString *patternEmail = @"\b([a-zA-Z0-9%_.+\-]+)@([a-zA-Z0-9.\-]+?\.[a-zA-Z]{2,6})\b";
// NSError *error = NULL;
//定义正则表达式
NSRegularExpression *regexQQ = [NSRegularExpressionregularExpressionWithPattern:patternQQ options:0error:&error];
NSRegularExpression *regexEmail = [NSRegularExpressionregularExpressionWithPattern:patternEmail options:0error:&error];
//使用正则表达式匹配字符
NSTextCheckingResult *isMatchQQ = [regexQQ firstMatchInString:stringC
options:0
range:NSMakeRange(0, [stringC length])];
NSTextCheckingResult *isMatchEmail = [regexEmail firstMatchInString:stringC
options:0
range:NSMakeRange(0, [stringC length])];
if (isMatchQQ || isMatchEmail)
{
NSLog(@"是 QQ或者邮箱");
}
else
{
NSLog(@"不是 QQ或者邮箱");
}