Search code examples
qtqregexp

QRegExp and Null Character in Qt


i want search in a binary file with regular expression. my search is successful in Text files, but not match in binary file, because QRegExp in function indexIn stop search when meet the NULL Character (chr(0)). what can i do to solve this problem?


Solution

  • QString can contain null characters, it's just its constructors that are inconsistent...

    QString::fromUtf8(const char *str, int size = -1) uses the given size, while QString::fromUtf8(const QByteArray &str) forces a strlen instead of using the bytearray size. See for yourself Qt code.

    QRegExp also supports null characters:

    QString s(QChar(0));
    QRegExp re(s);
    qDebug() << re.indexIn(s); // will print 0, not -1