Is there any difference between QRegularExpression and QRegExp?

前端 未结 2 1221
面向向阳花
面向向阳花 2021-01-03 17:48

I see there is a new class for regular expressions - QRegularExpression. Is it just a typedef for QRegExp, or a new class, or what? And why do we need it, we al

2条回答
  •  鱼传尺愫
    2021-01-03 18:09

    At least for Qt 4.8. I can give a very practical reason to use QRegularExpressions instead of QRegExp:

    Do these look dangerous to you?

    int index = myQString.indexOf(myQRegExp);
    bool okay = myQString.contains(myQRegExp);
    

    Both lines can corrupt your heap, crash or hang your application. I experienced heap corruption and hang with Qt 4.8. The blog post QString::indexOf() versus Qt 4.5 explains that QString::indexOf() modifies a const QRegExp object. QString::contains() inlines QString::indexOf() so it's the same problem.

    If you're stuck with Qt4 and thus QRegExp, you could use

    int index = myQRegExp.indexIn(myQString);
    bool okay = (myQRegExp.indexIn(myQString) != -1); 
    

    in your sources instead. Or patch the Qt Sources.

提交回复
热议问题