//NSString *string = @"028-909-0980";

//Apple also provides a way to use ICU based regular expressions from Foundation 

//by adding a new option to NSStringCompareOptions– NSRegularExpressionSearch. 

//?is new option can be used with the NSString rangeOfString:options: method,

//and the following example of its usage is given:

// finds phone number in format nnn-nnn-nnnn

/*NSRange r; 

NSString *regEx = @"[0-9]{3}-[0-9]{3}-[0-9]{4}"; //0-9  3   0-9 3  0-9 4

r = [string rangeOfString:regEx options:NSRegularExpressionSearch]; 

if (r.location != NSNotFound) {

NSLog(@"Phone number is %@", [string substringWithRange:r]); 

}

else {

NSLog(@"Not found.");

}

*/

//What's more, RegexKitLite provides easy access to 

//all the matches of a regular expression in a NSString:

// finds phone number in format nnn-nnn-nnnn

//NSString *regEx = @"[0-9]{3}-[0-9]{3}-[0-9]{4}"; 

 

 

/*for(NSString *match in [string componentsMatchedByRegex:regEx]) {

NSLog(@"Phone number  2222 is %@", match);

}

*/

//?e following example demonstrates how to match several 

//?elds in a URL and create a NSDictionary with the extracted results. 

//Only the capture groups that result in a successful match 

//will create a corresponding key in the dictionary.

//NSString *searchString = @"http://johndoe:secret@www.example.com:8080/private/mail/index.html";

// NSString *regexString = @"\\b(https?)://(?:(\\S+?)(?::(\\S+?))?@)?([a-zA-Z0-9\\-.]+)" @"(?::(\\d+))?((?:/[a-zA-Z0-9\\-._?,'+\\&%$=~*!():@\\\\]*)+)?";

// if([searchString isMatchedByRegex:regexString]) { 

//

// NSString *protocolString = [searchString stringByMatching:regexString capture:1L];

// NSString *userString = [searchString stringByMatching:regexString capture:2L];

// NSString *passwordString = [searchString stringByMatching:regexString capture:3L];

// NSString *hostString = [searchString stringByMatching:regexString capture:4L];

// NSString *portString   = [searchString stringByMatching:regexString capture:5L];

// NSString *pathString  = [searchString stringByMatching:regexString capture:6L];

// NSMutableDictionary *urlDictionary = [NSMutableDictionary dictionary];

//

// }

//NSString *searchString = @"http://johndoe:secret@www.example.com:8080/private/mail/index.html";

// NSString *regexString = @"\\b(https?)://(?:(\\S+?)(?::(\\S+?))?@)?([a-zA-Z0-9\\-.]+)"

// @"(?::(\\d+))?((?:/[a-zA-Z0-9\\-._?,'+\\&%$=~*!():@\\\\]*)+)?"; 

// NSDictionary *urlDictionary = NULL;

// urlDictionary = [searchString dictionaryByMatchingRegex:regexString 

// withKeysAndCaptures:

// @"protocol", 1,

// @"user", 2,

// @"password", 3, 

// @"host", 4, 

// @"port", 5, 

// @"path", 6,NULL];

// if(urlDictionary != NULL) {

//

// NSLog(@"urlDictionary: %@", urlDictionary);

// }

//

/*

HTTP

\bhttps?://[a-zA-Z0-9\-.]+↩ (?:(?:/[a-zA-Z0-9\-._?,'+&%$= *!():@\\]*)+)?

HTTP

\b(https?)://([a-zA-Z0-9\-.]+)↩ ((?:/[a-zA-Z0-9\-._?,'+&%$= *!():@\\]*)+)?

HTTP

\b(https?)://(?:(\S+?)(?::(\S+?))?@)?([a-zA-Z0-9\-.]+)↩ (?::(\d+))?((?:/[a-zA-Z0-9\-._?,'+&%$= *!():@\\]*)+)?

E-Mail

\b([a-zA-Z0-9%_.+\-]+)@([a-zA-Z0-9.\-]+?\.[a-zA-Z]{2,6})\b

Hostname

\b(?:[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}?[a-zA-Z0-9]\.)+[a-zA-Z]{2,6}\b

IP

\b(?:\d{1,3}\.){3}\d{1,3}\b

IP with Optional Netmask

\b((?:\d{1,3}\.){3}\d{1,3})(?:/(\d{1,2}))?\b

IP or Hostname

\b(?:(?:\d{1,3}\.){3}\d{1,3}|(?:[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}?↩ [a-zA-Z0-9]\.)+[a-zA-Z]{2,6})\b

*/

/*NSString *url = @"http://www.baidu.com哈哈哈";

NSString *regexString = @"\\b(https?)://(?:(\\S+?)(?::(\\S+?))?@)?([a-zA-Z0-9\\-.]+)"

@"(?::(\\d+))?((?:/[a-zA-Z0-9\\-._?,'+\\&%$=~*!():@\\\\]*)+)?";

  \\b(https?)://(?:(\\S+?)(?::(\\S+?))?@)?([a-zA-Z0-9\\-.]+)↩ (?::(\d+))?((?:/[a-zA-Z0-9\-._?,'+&%$= *!():@\\]*)+)?

NSString *userString = [url stringByMatching:regexString capture:4L];

NSLog(@" userString  %@",userString);

NSArray *itemArray = [url componentsSeparatedByString:userString];

NSLog(@"itemArray  %@",itemArray);

NSRange range = [url rangeOfString:userString];

if (range.location!=NSNotFound) {

NSLog(@"location   : %d length:  %d",range.location,range.length);

}

else {

NSLog(@"not find");

}

NSLog(@"前面: %@ 中间: %@ 后面:  %@",[url substringToIndex:range.location],

  [url substringWithRange:range],

  [url substringFromIndex:range.location+range.length]);

 

*/

NSString *url = @"哈哈哈http://scxixi.blog.51cto.com/4219352/987937.哈哈哈";

NSString *regexString = @"\\b(?:[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}?[a-zA-Z0-9]\\.)+[a-zA-Z]{2,6}\\b";

NSString *result = [url stringByMatching:regexString];

NSLog(@"result  %@",result);

NSRange range = [url rangeOfString:@"http://"];

NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];

if (range.location!=NSNotFound) {

NSRange r = [url rangeOfString:result];

if (r.location!=NSNotFound) {

NSString *s = [[NSString alloc] initWithFormat:@"http://%@",result];

NSRange ranges = [url rangeOfString:s];

[dic setObject:[url substringToIndex:range.location] forKey:@"left"];

[dic setObject:[url substringWithRange:ranges] forKey:@"url"];

[dic setObject:[url substringFromIndex:

[[url substringToIndex:range.location] length]+ranges.length

forKey:@"right"];

}

else {

NSLog(@"error");

dic = nil;

}

 

}

else {

NSRange r = [url rangeOfString:result];

if (r.location!=NSNotFound) {

[dic setObject:[url substringToIndex:r.location] forKey:@"left"];

[dic setObject:[url substringWithRange:r] forKey:@"url"];

[dic setObject:[url substringFromIndex:r.location+r.length] forKey:@"right"];

}

else {

NSLog(@"error");

dic = nil;

}

}

 

//NSLog(@"dic  %@",dic);