1

I'm doing my miniSQL and trying to use regex to parse query statements.

Though succeeded in handling the case "create table a(a int, b int)", I failed to handle to case when nested brackets existing.

I'm just wondering what's wrong with my code : P

int main()
{
//  std::string first = "create table a(a int, b1 int)"; it prints (a int, b1 int), works!
    std::string cmd = "create table a(a int, b1 int, c char(20))"; // failed to prints (20) only
    const regex pattern("[\\(]((\\w+)|(,)|(\\s+))*[\\)]");
    const sregex_token_iterator end;

    for (sregex_token_iterator it(cmd.begin(), cmd.end(), pattern); it != end; it ++ )
    {
        cout << *it << endl;
    }

    return 0;
}
Qi W.
  • 646
  • 9
  • 21
  • 6
    Regular expressions aren't suitable for parsing nested constructs, you need a parser for this. – Ibrahim Najjar Nov 01 '13 at 12:00
  • 2
    See also: http://stackoverflow.com/questions/133601/can-regular-expressions-be-used-to-match-nested-patterns – Dan Nov 01 '13 at 12:01
  • 2
    To be precise, regular expressions are incapable of parsing nested constructs with a possibility of infinite levels of nesting. If you define a limit to the number of nested levels, regexes *can* do it. Whether it's a good idea is another story, though. – Eric Finn Nov 01 '13 at 12:14

0 Answers0