Reputation: 14404
I want to extract the number 81698
from the string below, however I am running into some difficulties.
Here is my code.
NSString *content = @"... list.vars.results_thpp = 32;
list.vars.results_count = 81698;
list.vars.results_board = '12'; ...";
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"results_count.*[0-9*];"
options:NSRegularExpressionCaseInsensitive
error:&error];
NSString *modifiedString = [regex stringByReplacingMatchesInString:content
options:0
range:NSMakeRange(0, [content length])
withTemplate:@"$1"];
The output is this
... list.vars.results_thpp = 32;
list.vars.
list.vars.results_board = '12'; ...
But I just want 81698
What am I doing wrong?
Upvotes: 0
Views: 203
Reputation: 6718
NSString *content = @"... list.vars.results_thpp = 32;list.vars.results_count = 81698;list.vars.results_board = '12'; ...";
NSString *param = nil;
NSRange start = [content rangeOfString:@"list.vars.results_count = "];
if (start.location != NSNotFound) {
param = [content substringFromIndex:start.location + start.length];
NSRange end = [param rangeOfString:@";"];
if (end.location != NSNotFound) {
param = [param substringToIndex:end.location];
}
}
NSLog(@"param:%@", param);
I think it will be helpful to you.
Upvotes: 2