Qi W.
Qi W.

Reputation: 786

How to use sregex_token_iterator

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: enter image description here

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?

Upvotes: 4

Views: 8322

Answers (1)

Igor Tandetnik
Igor Tandetnik

Reputation: 52471

sregex_token_iterator stores a pointer to pattern, not a copy. You are changing the regular expression right from under the iterator.

Upvotes: 4

Related Questions