Comment écrire des expressions régulières dans Objective C (NSRegularExpression)?

J'ai cette regex qui fonctionne quand je la teste en PHP mais ça ne marche pas en Objective C:

(?:www\.)?((?!-)[a-zA-Z0-9-]{2,63}(?<!-))\.?((?:[a-zA-Z0-9]{2,})?(?:\.[a-zA-Z0-9]{2,})?) 

J'ai essayé d'échapper aux caractères d'échappement, mais cela n'aide pas non plus. Devrais-je échapper à un autre personnage?

C'est mon code en Objective C:

 NSMutableSsortingng *searchedSsortingng = [NSMutableSsortingng ssortingngWithSsortingng:@"domain-name.tld.tld2"]; NSError* error = nil; NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:@"(?:www\\.)?((?!-)[a-zA-Z0-9-]{2,63}(?<!-))\\.?((?:[a-zA-Z0-9]{2,})?(?:\\.[a-zA-Z0-9]{2,})?)" options:0 error:&error]; NSArray* matches = [regex matchesInSsortingng:searchedSsortingng options:0 range:NSMakeRange(0, [searchedSsortingng length])]; for ( NSTextCheckingResult* match in matches ) { NSSsortingng* matchText = [searchedSsortingng subssortingngWithRange:[match range]]; NSLog(@"match: %@", matchText); } 

— METTRE À JOUR —

Cette regex returnne (en PHP) le tableau avec les valeurs "domain-name" et "tld.tld2" mais en Objective C je n'ai qu'une seule valeur: "domain-name.tld.tld2"

– MISE À JOUR 2 –

Cette expression régulière extrait «nom de domaine» et «TLD» de la string:

  • domain.com = (domaine, com)
  • domain.co.uk = (domaine, co.uk)
  • -test-domain.co.u = (domaine de test, co)
  • -test-domain.co.uk- = (domaine de test, co.uk)
  • -test-domain.co.uk = (domaine de test, co)
  • -test-domain.co-m = (domaine de test)
  • -test-domain-.co.uk = (domaine de test)

il prend le nom de domaine valide (ne commençant pas ou se terminant par '-' et entre 2 et 63 caractères), et jusqu'à deux parties d'un TLD si les parties sont valides (au less deux caractères contenant uniquement des lettres et des numbers)

J'espère que cette explication aide.

Un NSTextCheckingResult possède plusieurs éléments obtenus en l'indexant.

[match rangeAtIndex:0]; est le match complet.
[match rangeAtIndex:1]; (si elle existe) est la première correspondance de groupe de capture.
etc.

Vous pouvez utiliser quelque chose comme ceci:

 NSSsortingng *searchedSsortingng = @"domain-name.tld.tld2"; NSRange searchedRange = NSMakeRange(0, [searchedSsortingng length]); NSSsortingng *pattern = @"(?:www\\.)?((?!-)[a-zA-Z0-9-]{2,63}(?<!-))\\.?((?:[a-zA-Z0-9]{2,})?(?:\\.[a-zA-Z0-9]{2,})?)"; NSError *error = nil; NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern: pattern options:0 error:&error]; NSArray* matches = [regex matchesInSsortingng:searchedSsortingng options:0 range: searchedRange]; for (NSTextCheckingResult* match in matches) { NSSsortingng* matchText = [searchedSsortingng subssortingngWithRange:[match range]]; NSLog(@"match: %@", matchText); NSRange group1 = [match rangeAtIndex:1]; NSRange group2 = [match rangeAtIndex:2]; NSLog(@"group1: %@", [searchedSsortingng subssortingngWithRange:group1]); NSLog(@"group2: %@", [searchedSsortingng subssortingngWithRange:group2]); } 

Sortie NSLog:

Correspondance: domain-name.tld.tld2
nom de domaine
tld.tld2

Vérifiez que les plages de correspondance sont valides.

Plus simplement dans ce cas:

 NSSsortingng *searchedSsortingng = @"domain-name.tld.tld2"; NSRange searchedRange = NSMakeRange(0, [searchedSsortingng length]); NSSsortingng *pattern = @"(?:www\\.)?((?!-)[a-zA-Z0-9-]{2,63}(?<!-))\\.?((?:[a-zA-Z0-9]{2,})?(?:\\.[a-zA-Z0-9]{2,})?)"; NSError *error = nil; NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:&error]; NSTextCheckingResult *match = [regex firstMatchInSsortingng:searchedSsortingng options:0 range: searchedRange]; NSLog(@"group1: %@", [searchedSsortingng subssortingngWithRange:[match rangeAtIndex:1]]); NSLog(@"group2: %@", [searchedSsortingng subssortingngWithRange:[match rangeAtIndex:2]]); 

Swift 3.0:

 let searchedSsortingng = "domain-name.tld.tld2" let nsSearchedSsortingng = searchedSsortingng as NSSsortingng let searchedRange = NSMakeRange(0, searchedSsortingng.characters.count) let pattern = "(?:www\\.)?((?!-)[a-zA-Z0-9-]{2,63}(?<!-))\\.?((?:[a-zA-Z0-9]{2,})?(?:\\.[a-zA-Z0-9]{2,})?)" do { let regex = try NSRegularExpression(pattern:pattern, options: []) let matches = regex.matches(in:searchedSsortingng, options:[], range:searchedRange) for match in matches { let matchText = nsSearchedSsortingng.subssortingng(with:match.range); print("match: \(matchText)"); let group1 : NSRange = match.rangeAt(1) let matchText1 = nsSearchedSsortingng.subssortingng(with: group1) print("matchText1: \(matchText1)") let group2 = match.rangeAt(2) let matchText2 = nsSearchedSsortingng.subssortingng(with: group2) print("matchText2: \(matchText2)") } } catch let error as NSError { print(error.localizedDescription) } 

sortie d'printing:

Correspondance: domain-name.tld.tld2
matchText1: nom de domaine
matchText2: tld.tld2

Plus simplement dans ce cas:

 do { let regex = try NSRegularExpression(pattern:pattern, options: []) let match = regex.firstMatch(in:searchedSsortingng, options:[], range:searchedRange) let matchText1 = nsSearchedSsortingng.subssortingng(with: match!.rangeAt(1)) print("matchText1: \(matchText1)") let matchText2 = nsSearchedSsortingng.subssortingng(with: match!.rangeAt(2)) print("matchText2: \(matchText2)") } catch let error as NSError { print(error.localizedDescription) } 

sortie d'printing:

matchText1: nom de domaine
matchText2: tld.tld2

Selon la documentation d' Apple , ces caractères doivent être cités (en utilisant \) pour être traités comme des littéraux:

 * ? + [ ( ) { } ^ $ | \ . / 

Cela aiderait aussi si vous pouviez expliquer ce que vous essayez d'accomplir. Avez-vous des appareils de test?