4

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?

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. In g(), I ues variable patternInner instead of the pattern in for loop, and it works!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.