Reputation: 605
I have varius log files to read. Each logs contain a report of a devices (printers). What I can find is always the word 'firmware:' followed by the firmware revision like:
PTRE firmware: XER8673B2
The log does not seem to be very ordered, whereby the position of this text is not always on the same point or on the same line, but is always in the "PTRE firmawre: XXXXXXX" format. How can I find XER8673B2 ? Any help is appreciated.
SOLVED (thanks to @roman-sausarnes), this is the code:
NSString *stringToSearch = [[NSString alloc] initWithContentsOfFile:@"path/to/log" encoding:NSUTF8StringEncoding error:nil];
NSString *preMatchString = @"PTRE firmware: ";
NSString *terminatingCharacter = @" ";
NSString *result = [[NSString alloc] init];
NSScanner *scanner = [NSScanner scannerWithString:stringToSearch];
[scanner scanUpToString:preMatchString intoString:NULL];
[scanner scanString:preMatchString intoString:NULL];
[scanner scanUpToString:terminatingCharacter intoString:&result];
NSLog(@"It's : %@", result);
The output is
It's : XER8673B2
Upvotes: 0
Views: 43
Reputation: 13316
Look at NSScanner. The code would look something like this:
NSString *stringToSearch = theStringThatYouWantToSearch;
NSString *preMatchString = @"firmware: ";
NSString *terminatingCharacter = " ";
NSString *result = [[NSString alloc] init];
NSScanner *scanner = [NSScanner scannerWithString:stringToSearch];
[scanner scanUpToString:preMatchString intoString:NULL];
[scanner scanString:preMatchString intoString:NULL];
[scanner scanUpToString:terminatingCharacter intoString:&result];
At the end, result
should be the string that came after "firmware: " but before the next trailing space (" ").
Upvotes: 1