0

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?

4

1 回答 1

2
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);

我想这会对你有所帮助。

于 2012-11-27T06:00:02.980 回答