2

I'm making a regular expression for the following line:

Table 'Joella VIII' 6-max Seat #4 is the button

So far, I've got this:

self.tableDetailsRegex = [NSRegularExpression regularExpressionWithPattern:@"Table '[A-Za-z0-9 ]*' [0-9]+-max Seat #[0-9]+ is the button" options:NSRegularExpressionAllowCommentsAndWhitespace error:nil];

if([self.tableDetailsRegex numberOfMatchesInString:line options:NSMatchingReportCompletion range:NSMakeRange(0, line.length)] == 1)
{
    NSLog(@"%@", line);
}

So, my regular expression is:

Table '[A-Za-z0-9 ]*' [0-9]+-max Seat #[0-9]+ is the button

And I'm sure the selected line comes by at some point, because I'm printing all the lines a bit further in my code...

Sander Declerck
  • 2,455
  • 4
  • 28
  • 38
  • FWIW, if I copy/paste your pattern and test string into my tool, it does match. Problem relative to the `if`? – Seki Jan 20 '12 at 16:24

2 Answers2

3

Your regular expression does match your string. Try it in this online matcher.

The problem is the option you pass: NSRegularExpressionAllowCommentsAndWhitespace which causes the match to ignore white space and # signs plus anything following the # in the regular expression, which you don't want. Pass zero for the options.

JeremyP
  • 84,577
  • 15
  • 123
  • 161
2

Your problem is in the options you are using. From the NSRegularExpression Class Reference, NSRegularExpressionAllowCommentsAndWhitespace means that whitespace and anything after a # in the regular expression will be ignored. With that option enabled, the regular expression acts like this:

Table'[A-Za-z0-9]*'[0-9]+-maxSeat

You probably want to pass 0 for the options, so that none of them get enabled.

self.tableDetailsRegex = [NSRegularExpression regularExpressionWithPattern:@"Table '[A-Za-z0-9 ]*' [0-9]+-max Seat #[0-9]+ is the button" options:0 error:nil];
ughoavgfhw
  • 39,734
  • 6
  • 101
  • 123