Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Read data from double quotes txt file.
Qt 6.12 LTS is out! See what's new in the release blog

Read data from double quotes txt file.

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 1 Posters 474 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pedromenezes
    wrote on last edited by
    #1

    Hi Guys, can someone help me?

    I would like to read a TXT file and extract some data that are in double quotes. I managed to make the code by reading other forums. It works for the first data found. I would like to continue finding for another data on the same line.

    //Here is the TXT file content:

    sd sdkfjsld lksjdfls pon. 234234; sd;flksd "74.4" lkasjdlsk aslfksjdfls sdlkfjsldf sldkfjsld 342352 ds;flsdkf "256" "sdlkfjds
    2873ihwefisjhfs dlkfjlsd fgdf090324i2. 43p530i953kldf,msd ; lsdjkflskfn fdlsmkfsd "8" ..,lmsdlifjodsf sokdjflsdn
    0.01
    0.02
    0.03
    0.04

    //The code:

    void MainWindow::Convert () {

    QString filter = "All file (*.txt);; Text file (*.txt)";
    QString file_name = QFileDialog::getOpenFileName(this, "Upload file",QDir::homePath(), filter);
    QFile file (file_name);
    
    const QRegularExpression regex("\"([A-Za-z0-9_\\./\\-\\s]*)\"");
    
    if(file.open(QFile::ReadOnly | QFile::Text)){
        QTextStream in(&file);
    
         qDebug() << "Opened file:" << file;
    
        while(!in.atEnd()){
            const QString lineString = in.readLine(); // read line
    
            qDebug() << "LineString" << lineString;
    
            const QRegularExpressionMatch match = regex.match(lineString); // apply the pattern on Text
    
            if(match.hasMatch()){ // if captured the words
                qDebug() << "Full Capture: " << match.captured(0);
                qDebug() << "Captured Words: " << match.captured(1) << match.captured(2) << match.captured(3) << match.captured(4);
            }
        }
        file.close();
    }
    

    }

    //Output:
    Opened file: QFileInfo(/Users/../Desktop/Tests.txt)
    LineString "sd sdkfjsld lksjdfls pon. 234234; sd;flksd "74.4" lkasjdlsk\taslfksjdfls\tsdlkfjsldf\tsldkfjsld 342352 ds;flsdkf "256"\t"sdlkfjds"
    Full Capture: ""74.4""
    Captured Words: "74.4" "" "" ""
    LineString "2873ihwefisjhfs dlkfjlsd fgdf090324i2. 43p530i953kldf,msd ;\tlsdjkflskfn\tfdlsmkfsd\t"8" ..,lmsdlifjodsf sokdjflsdn \t"
    Full Capture: ""8""
    Captured Words: "8" "" "" ""
    LineString "0.01\t\t\t\t"
    LineString "0.02\t\t\t\t"
    LineString "0.03\t\t\t\t"
    LineString "0.04\t\t\t\t"

    Thank you very much

    1 Reply Last reply
    0
    • P Offline
      P Offline
      pedromenezes
      wrote on last edited by pedromenezes
      #2

      Solved:

              QRegularExpressionMatchIterator i = regex.globalMatch(lineString);
      
              while (i.hasNext()) {
                  QRegularExpressionMatch match = i.next();
                  if (match.hasMatch()) {
                       qDebug().noquote() <<"Value"<< match.captured(0);
      

      }

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt
      • Unsolved