Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think the best way to find the match is by using regulars expressions with <strong>NSRegularExpression</strong>.</p> <pre><code>NSString * stringEx=@"www.mysite.com/word-4-word-1-1-word-word-2-word-817061.html"; NSError *error = NULL; NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"-(\\d*).html$" options:NSRegularExpressionCaseInsensitive error:&amp;error]; NSArray *matches = [regex matchesInString:stringEx options:NSMatchingReportCompletion range:NSMakeRange(0, [stringEx length])]; if ([matches count] &gt; 0) { NSString* resultString = [stringEx substringWithRange:[matches[0] rangeAtIndex:1]]; NSLog(@"Matched: %@", resultString); } </code></pre> <p>Make sure you use an extra <code>\</code> escape character in the regex <code>NSString</code> whenever needed.</p> <p><strong>UPDATE</strong></p> <p>I did a test using the two different approaches (regex vs string splitting) with the code below:</p> <pre><code>NSDate *timeBefore = [NSDate date]; NSError *error = NULL; NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"-(\\d*).html$" options:NSRegularExpressionCaseInsensitive error:&amp;error]; for (int i = 0; i &lt; 100000; i++) { NSArray *matches = [regex matchesInString:stringEx options:NSMatchingReportCompletion range:NSMakeRange(0, [stringEx length])]; if ([matches count] &gt; 0) { NSString* resultString = [stringEx substringWithRange:[matches[0] rangeAtIndex:1]]; } } NSTimeInterval timeSpent = [timeBefore timeIntervalSinceNow]; NSLog(@"Time: %.5f", timeSpent*-1); </code></pre> <p>on the simulator the differences are not significant, but running on an iPhone 4 I got the following results:</p> <pre><code>2013-11-25 10:24:19.795 NotifApp[406:60b] Time: 11.45771 // string splitting 2013-11-25 10:25:10.451 NotifApp[412:60b] Time: 7.55713 // regex </code></pre> <p>so I guess the best approach depends on case to case.</p>
 

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