Reputation: 3113
In the following code I am attempting to use regex to extract portions of the text file farther below.
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);
NSString *string = [[NSString alloc] initWithData:receivedData encoding:NSISOLatin1StringEncoding];
NSLog(@"string length: %d", [string length]);
NSError *error = nil;
NSString *toMatch = @"\[Board\\t\"([0-9]?)\"]*\[Dealer\\t\"([NEWS])\"]*";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:toMatch
options:0 error:&error];
NSLog(@"length: %d", [toMatch length]);
NSUInteger numberOfMatches = [regex numberOfMatchesInString:string options:0 range:NSMakeRange(0, [string length])];
NSLog(@" %ud", numberOfMatches);
for (NSTextCheckingResult* match in [regex matchesInString:string options:0 range:NSMakeRange(0, [string length])]){
// cannot make this work: NSRange trange =[match range];
// cannot make this work: NSLog(@"range %i,%i", trange );
NSString* tstring=[string substringWithRange:trange];
NSLog(@" %@", tstring );}
}
I am using NSRegularExpression
to pick out information from the excerpted following text. More particularly I need the Board
number and the Dealer
values for each board (there are about 40 boards and I have deleted several irrelevant rows in the listing).
[Board "1"]
[Dealer "N"]
[Vulnerable "None"]
[Deal "N:Q952.652.KJT4.95 T.KQT84.A865.J73 K8763.A7.Q.KQT84 AJ4.J93.9732.A62"]
[Scoring ""]
[Declarer ""]
[Contract ""]
[Board "2"]
[Dealer "E"]
[Vulnerable "NS"]
[Deal "E:K8542.3.4.AT7532 J76.K7.AT85.KQJ8 QT3.AJ84.KJ963.4 A9.QT9652.Q72.96"]
[Scoring ""]
[Declarer ""]
[Contract ""]
I am getting gobbledygook for my for
loop print out. There are at least two reasons for the gobbledygook: my regex expression is wrong, or my for
loop is wrong.
Console output and gobbledygook
follows.
2013-02-03 11:00:14.161 BridgeDuplicate[51867:11303] the window: <UIWindow: 0x956eac0; frame = (0 0; 768 1024); hidden = YES; layer = <UIWindowLayer: 0x956ebc0>>
2013-02-03 11:00:14.163 BridgeDuplicate[51867:11303] the rootViewController: <BSViewController: 0x7188220>
2013-02-03 11:00:14.166 BridgeDuplicate[51867:11303] viewDidLoad
2013-02-03 11:00:27.156 BridgeDuplicate[51867:11303] Succeeded! Received 303896 bytes of data
2013-02-03 11:00:27.158 BridgeDuplicate[51867:11303] string length: 303896
2013-02-03 11:00:27.164 BridgeDuplicate[51867:11303] length: 41
2013-02-03 11:00:27.205 BridgeDuplicate[51867:11303] 264765d
2013-02-03 11:00:27.205 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.206 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.206 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.206 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.206 BridgeDuplicate[51867:11303] l
2013-02-03 11:00:27.206 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.206 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.206 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.206 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.206 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.207 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.207 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.207 BridgeDuplicate[51867:11303] ea
2013-02-03 11:00:27.207 BridgeDuplicate[51867:11303] d
2013-02-03 11:00:27.207 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.207 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.207 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.207 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.207 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.208 BridgeDuplicate[51867:11303] e
2013-02-03 11:00:27.208 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.208 BridgeDuplicate[51867:11303] a
2013-02-03 11:00:27.208 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.208 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.208 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.208 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.208 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.208 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.208 BridgeDuplicate[51867:11303] e
2013-02-03 11:00:27.209 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.209 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.209 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.209 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.209 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.209 BridgeDuplicate[51867:11303] "
2013-02-03 11:00:27.228 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.228 BridgeDuplicate[51867:11303] o
2013-02-03 11:00:27.228 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.228 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.228 BridgeDuplicate[51867:11303] e
2013-02-03 11:00:27.228 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.229 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.229 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.229 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.229 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.229 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.230 BridgeDuplicate[51867:11303] e"
2013-02-03 11:00:27.230 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.230 BridgeDuplicate[51867:11303]
Upvotes: 0
Views: 130
Reputation: 29886
I suspect you're misunderstanding how NSTextCheckingResult
works, but perhaps more importantly, there are some issues with your pattern. The following code should be illustrative:
NSString *string = @"[Board\t\"1\"]\n[Dealer\t\"N\"]\n";
NSLog(@"string length: %lu", (unsigned long)[string length]);
NSError *error = nil;
NSString *toMatch = @"\\[Board\\t\"([0-9]?)\"\\].*\\n\\[Dealer\\t\"([NEWS])\"\\].*";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:toMatch options:0 error:&error];
NSLog(@"length: %lu", (unsigned long)[toMatch length]);
NSUInteger numberOfMatches = [regex numberOfMatchesInString:string options:0 range:NSMakeRange(0, [string length])];
NSLog(@"number of matches: %lu", (unsigned long)numberOfMatches);
for (NSTextCheckingResult* match in [regex matchesInString:string options:0 range:NSMakeRange(0, [string length])])
{
NSLog(@"Number of ranges in match: %lu", match.numberOfRanges);
for (NSUInteger i = 0; i < match.numberOfRanges; ++i)
{
NSRange matchedRange = [match rangeAtIndex: i];
NSString* tstring = [string substringWithRange: matchedRange];
NSLog(@"range %lu string: %@", (unsigned long)i, tstring);
}
}
What you'll get out of this is:
2013-02-03 12:16:41.112 RegExTest[72290:303] string length: 25
2013-02-03 12:16:43.889 RegExTest[72290:303] length: 49
2013-02-03 12:16:43.889 RegExTest[72290:303] number of matches: 1
2013-02-03 12:16:43.890 RegExTest[72290:303] Number of ranges in match: 3
2013-02-03 12:16:43.890 RegExTest[72290:303] range 0 string: [Board "1"]
[Dealer "N"]
2013-02-03 12:16:43.890 RegExTest[72290:303] range 1 string: 1
2013-02-03 12:16:43.890 RegExTest[72290:303] range 2 string: N
The thing to know here is that you're shooting for one match, and for that match to have multiple ranges. Every successful match has at least one range: the range of the entire string that was matched by the entire pattern (which is not what you're interested in here.) The parenthesis-based capture groups will appear at indexes beyond 0, as shown by this code.
The escaping rules are kind of a pain -- there's rules for escaping in an NSString, and then the rules for escaping a regular expression. It can be non-obvious how they interact, but the pattern I came up with here seems to do what you're after.
EDIT:
Here's another version that pulls directly from your URL and successfully matches:
NSError* error = nil;
NSString* string = [NSString stringWithContentsOfURL: [NSURL URLWithString: @"http://www.atlantaduplicatebridgeclub.com/scorepost/2013/01/20130126ana.pbn"]
encoding: NSUTF8StringEncoding error: &error];
NSLog(@"string length: %lu", (unsigned long)[string length]);
NSString *toMatch = @"\\[Board\\s*\"([0-9]?)\"\\].*\\[Dealer\\s*\"([NEWS])\"\\]";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:toMatch options:NSRegularExpressionDotMatchesLineSeparators error:&error];
NSLog(@"pattern length: %lu", (unsigned long)[toMatch length]);
NSUInteger numberOfMatches = [regex numberOfMatchesInString:string options:0 range:NSMakeRange(0, [string length])];
NSLog(@"number of matches: %lu", (unsigned long)numberOfMatches);
for (NSTextCheckingResult* match in [regex matchesInString:string options:NSRegularExpressionDotMatchesLineSeparators range:NSMakeRange(0, [string length])])
{
NSLog(@"Number of ranges in match: %lu", match.numberOfRanges);
for (NSUInteger i = 0; i < match.numberOfRanges; ++i)
{
NSRange matchedRange = [match rangeAtIndex: i];
NSString* tstring = [string substringWithRange: matchedRange];
NSLog(@"range %lu string: %@", (unsigned long)i, tstring);
}
}
Upvotes: 1