Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A <code>NSTextCheckingResult</code> has multiple items obtained by indexing into it.</p> <p><code>[match rangeAtIndex:0];</code> is the full match.<br> <code>[match rangeAtIndex:1];</code> (if it exists) is the first capture group match.<br> etc. </p> <p>You can use something like this:</p> <pre><code>NSString *searchedString = @"domain-name.tld.tld2"; NSRange searchedRange = NSMakeRange(0, [searchedString length]); NSString *pattern = @"(?:www\\.)?((?!-)[a-zA-Z0-9-]{2,63}(?&lt;!-))\\.?((?:[a-zA-Z0-9]{2,})?(?:\\.[a-zA-Z0-9]{2,})?)"; NSError *error = nil; NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern: pattern options:0 error:&amp;error]; NSArray* matches = [regex matchesInString:searchedString options:0 range: searchedRange]; for (NSTextCheckingResult* match in matches) { NSString* matchText = [searchedString substringWithRange:[match range]]; NSLog(@"match: %@", matchText); NSRange group1 = [match rangeAtIndex:1]; NSRange group2 = [match rangeAtIndex:2]; NSLog(@"group1: %@", [searchedString substringWithRange:group1]); NSLog(@"group2: %@", [searchedString substringWithRange:group2]); } </code></pre> <p>NSLog output:</p> <blockquote> <p>match: domain-name.tld.tld2<br> domain-name<br> tld.tld2</p> </blockquote> <p>Do test that the match ranges are valid.</p> <p>More simply in this case:</p> <pre><code>NSString *searchedString = @"domain-name.tld.tld2"; NSRange searchedRange = NSMakeRange(0, [searchedString length]); NSString *pattern = @"(?:www\\.)?((?!-)[a-zA-Z0-9-]{2,63}(?&lt;!-))\\.?((?:[a-zA-Z0-9]{2,})?(?:\\.[a-zA-Z0-9]{2,})?)"; NSError *error = nil; NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:&amp;error]; NSTextCheckingResult *match = [regex firstMatchInString:searchedString options:0 range: searchedRange]; NSLog(@"group1: %@", [searchedString substringWithRange:[match rangeAtIndex:1]]); NSLog(@"group2: %@", [searchedString substringWithRange:[match rangeAtIndex:2]]); </code></pre> <blockquote> <p>Swift 3.0:</p> </blockquote> <pre><code>let searchedString = "domain-name.tld.tld2" let nsSearchedString = searchedString as NSString let searchedRange = NSMakeRange(0, searchedString.characters.count) let pattern = "(?:www\\.)?((?!-)[a-zA-Z0-9-]{2,63}(?&lt;!-))\\.?((?:[a-zA-Z0-9]{2,})?(?:\\.[a-zA-Z0-9]{2,})?)" do { let regex = try NSRegularExpression(pattern:pattern, options: []) let matches = regex.matches(in:searchedString, options:[], range:searchedRange) for match in matches { let matchText = nsSearchedString.substring(with:match.range); print("match: \(matchText)"); let group1 : NSRange = match.rangeAt(1) let matchText1 = nsSearchedString.substring(with: group1) print("matchText1: \(matchText1)") let group2 = match.rangeAt(2) let matchText2 = nsSearchedString.substring(with: group2) print("matchText2: \(matchText2)") } } catch let error as NSError { print(error.localizedDescription) } </code></pre> <p>print output: </p> <blockquote> <p>match: domain-name.tld.tld2<br> matchText1: domain-name<br> matchText2: tld.tld2 </p> </blockquote> <p>More simply in this case:</p> <pre><code>do { let regex = try NSRegularExpression(pattern:pattern, options: []) let match = regex.firstMatch(in:searchedString, options:[], range:searchedRange) let matchText1 = nsSearchedString.substring(with: match!.rangeAt(1)) print("matchText1: \(matchText1)") let matchText2 = nsSearchedString.substring(with: match!.rangeAt(2)) print("matchText2: \(matchText2)") } catch let error as NSError { print(error.localizedDescription) } </code></pre> <p>print output: </p> <blockquote> <p>matchText1: domain-name<br> matchText2: tld.tld2 </p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload