Problem with finding pattern using QFile[SOLVED]



  • Hello everyone!

    I have faced with some trouble while using next program:

    QFile file1("/Path/To/File/input.txt");
    file1.open(QIODevice::ReadOnly);
    QString s = file1.readAll();
    
    if (!file1.isOpen()) {
        qDebug() << "Not";
        return -1;
    }
    
    file1.close();
    
    
    QRegExp reg(QString("%1").arg("Слово"));
    
    QLabel l;
    l.setText(reg.lastIndexIn(s));
    l.show();
    

    I am trying to find word in my file input.txt. There is only one word "Слово". But every time I getting result -1.

    But in next program:

    QString s = QByteArray("Слово");
    
    QRegExp reg(QString("%1").arg("Слово"));
    
    QLabel l;
    l.setText(reg.lastIndexIn(s));
    l.show();
    

    The result is 0.

    Why the results are different? And what I need to do to get correct answer in first program?
    Thank in advance.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Why not just use QString's contains method ?



  • @SGaist Hello and thank you for answer.
    Thank for this method, but it doesn't help me with my problem.


  • Lifetime Qt Champion

    What's the encoding of input.txt ?

    By the way QRegExp rx("Слово"); is enough to create your QRegExp.

    On a side note, if you are using Qt 5, QRegularExpression is recommended.



  • @SGaist The Unicode encoding.
    So I need to use QTextStream to solve my problem.
    Thank for your help!


  • Lifetime Qt Champion

    You're welcome !

    Since you have it working now, please update the thread title prepending [solved] so other forum users may know a solution has been found :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.