Search code examples
c++qtqregexp

Qt regex not matching


Qt's regex (C++) is not working as I expect. For example, in the following line (spaces as full stops)

.....mRNA............complement(join(<85666..86403,86539..>86727))

"mRNA" is not matched by:

QRegExp rxItem("^\\s{5}(\\w+)") ;

But is matched by the following:

QRegExp rxItem("\\s{4}(\\w+)") ;

So it looks as if the start of the line and the first space is not being recognised for some reason. I checked-out the Qt documentation for QRegExp and as far as I can tell "^" signifies the start of a line and \s is space as in Perl.

Any ideas?

Cheers


Solution

  • The following code:

    #include <QtCore/QRegExp>
    #include <QtCore/QString>
    #include <QtCore/QDebug>
    
    int main(int argc, char *argv[])
    {
        QString test = "     mRNA            complement(join(<85666  86403,86539  >86727))";
        QRegExp rxItem( "^\\s{5}(\\w+)" );
    
        if( rxItem.indexIn( test ) != -1 )
        {
            qDebug() << "Matched" << rxItem.cap( 1 );
        }
        else
        {
            qDebug() << "No match";
        }
    
        return 0;
    }
    

    displays

    Matched "mRNA"
    

    So it seems to be working. Did you maybe treat a result of 0 returned by indexIn as error?