I'm trying to use regular expression to parse SQL statement while confused by the behavior of "sregex_token_iterator".
My function f() and g() looks similar while the former prints two sentences and the latter, g() prints one only:

Here is f():
void f()
{
    cout << "in f()" << endl;
    string str = " where a <= 2 and b = 2";
    smatch result;
    regex pattern("(\\w+\\s*(<|=|>|<>|<=|>=)\\s*\\w+)"); 
    const sregex_token_iterator end;
    for (sregex_token_iterator it(str.begin(), str.end(), pattern); it != end; it ++)
    {
        cout << *it << endl;
    }
}
Here is g():
void g()
{
    cout << "in g()" << endl;
    string str = " where a <= 2 and b = 2";
    smatch result;
    regex pattern("(\\w+\\s*(<|=|>|<>|<=|>=)\\s*\\w+)"); 
    const sregex_token_iterator end;
    for (sregex_token_iterator it(str.begin(), str.end(), pattern); it != end; it ++)
    {
        cout << *it << endl;
        string cur = *it;
        pattern = "(\\w+)\\s*<>\\s*(\\w+)";
        if ( regex_match(cur, result, pattern) )
        {
//          cout <<"<>" << endl;
        }
        pattern = "(\\w+)\\s*=\\s*(\\w+)";
        if ( regex_match(cur, result, pattern) ){}
        pattern = "(\\w+)\\s*<\\s*(\\w+)";
        if ( regex_match(cur, result, pattern) ){}
        pattern = "(\\w+)\\s*>\\s*(\\w+)";
        if ( regex_match(cur, result, pattern) ){}
        pattern = "(\\w+)\\s*<=\\s*(\\w+)";
        if ( regex_match(cur, result, pattern) ){}
        pattern = "(\\w+)\\s*>=\\s*(\\w+)";
        if ( regex_match(cur, result, pattern) ){}
    }
}
I'm guessing the variable 'end'("const sregex_token_iterator end;") changed in g() or the judge condition in "for" clause failed after it ++.
If it did, how did that happen. And what should I do to fix that?
sregex_token_iterator stores a pointer to pattern, not a copy. You are changing the regular expression right from under the iterator.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With