À l'aide de NSRegularExpression pour extraire les Url sur l'iPhone
Je suis en utilisant le code suivant sur mon iPhone, les prises de ici pour extraire toutes les URLs de rayures .code html.
Je suis le seul à être en mesure d'extraire la première URL, mais j'ai besoin d'un tableau contenant tous Url. Mon NSArray n'est pas de retour NSStrings pour chaque URL, mais les objets descriptions uniquement.
Comment puis-je faire mon arrayOfAllMatches
retour toutes les Url, comme NSStrings?
-(NSArray *)stripOutHttp:(NSString *)httpLine {
//Setup an NSError object to catch any failures
NSError *error = NULL;
//create the NSRegularExpression object and initialize it with a pattern
//the pattern will match any http or https url, with option case insensitive
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"http?://([-\\w\\.]+)+(:\\d+)?(/([\\w/_\\.]*(\\?\\S+)?)?)?" options:NSRegularExpressionCaseInsensitive error:&error];
//create an NSRange object using our regex object for the first match in the string httpline
NSRange rangeOfFirstMatch = [regex rangeOfFirstMatchInString:httpLine options:0 range:NSMakeRange(0, [httpLine length])];
NSArray *arrayOfAllMatches = [regex matchesInString:httpLine options:0 range:NSMakeRange(0, [httpLine length])];
//check that our NSRange object is not equal to range of NSNotFound
if (!NSEqualRanges(rangeOfFirstMatch, NSMakeRange(NSNotFound, 0))) {
//Since we know that we found a match, get the substring from the parent string by using our NSRange object
NSString *substringForFirstMatch = [httpLine substringWithRange:rangeOfFirstMatch];
NSLog(@"Extracted URL: %@",substringForFirstMatch);
NSLog(@"All Extracted URLs: %@",arrayOfAllMatches);
//return all matching url strings
return arrayOfAllMatches;
}
return NULL;
}
Voici mon NSLog de sortie:
Extracted URL: http://example.com/myplayer
All Extracted URLs: (
"<NSExtendedRegularExpressionCheckingResult: 0x106ddb0>{728, 53}{<NSRegularExpression: 0x106bc30> http?://([-\\w\\.]+)+(:\\d+)?(/([\\w/_\\.]*(\\?\\S+)?)?)? 0x1}",
"<NSExtendedRegularExpressionCheckingResult: 0x106ddf0>{956, 66}{<NSRegularExpression: 0x106bc30> http?://([-\\w\\.]+)+(:\\d+)?(/([\\w/_\\.]*(\\?\\S+)?)?)? 0x1}",
"<NSExtendedRegularExpressionCheckingResult: 0x106de30>{1046, 63}{<NSRegularExpression: 0x106bc30> http?://([-\\w\\.]+)+(:\\d+)?(/([\\w/_\\.]*(\\?\\S+)?)?)? 0x1}",
"<NSExtendedRegularExpressionCheckingResult: 0x106de70>{1129, 67}{<NSRegularExpression: 0x106bc30> http?://([-\\w\\.]+)+(:\\d+)?(/([\\w/_\\.]*(\\?\\S+)?)?)? 0x1}"
)
OriginalL'auteur Winston | 2012-03-06
Vous devez vous connecter pour publier un commentaire.
La méthode
matchesInString:options:range:
retourne un tableau deNSTextCheckingResult
objets. Vous pouvez utiliser rapide énumération pour parcourir le tableau, tirez la chaîne de chaque match à partir de votre chaîne d'origine, et ajouter de la sous-chaîne à un nouveau tableau.Fonctionne de manière impressionante! Est-Il possible d'obtenir le titre d'après l' > <. comme si l'url est <a href="Google" >google.com">Google</a> vous pourriez obtenir "Google"?
OriginalL'auteur jonkroll
Essayer NSDataDetector
OriginalL'auteur rudensm
Avec
NSDataDetector
l'utilisation de Swift :Je ne pense pas que vous avez besoin de la . reportCompletion option depuis votre utilisez pas enumerateMatches(dans:options:gamme:aide:)
OriginalL'auteur Victor Sigler
pour obtenir tous les liens d'une chaîne donnée
OriginalL'auteur Hossam Ghareeb
Je me suis trouvé tellement écœurés par la complexité de cette opération simple ("correspond à TOUTES les sous-chaînes") que j'ai fait une petite bibliothèque, je suis humblement appel Unsuck qui ajoute un peu de raison à
NSRegularExpression
dans la forme defrom
etallMatches
méthodes. Voici comment vous pouvez les utiliser:Veuillez découvrez la unsuck le code source sur github et dites-moi toutes les choses que j'ai fait de mal 🙂
Noter que
(?i)
le rend insensible à la casse, donc vous n'avez pas besoin de spécifierNSRegularExpressionCaseInsensitive
.OriginalL'auteur AlexChaffee