Read data from double quotes txt file.
-
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
-
Solved:
QRegularExpressionMatchIterator i = regex.globalMatch(lineString); while (i.hasNext()) { QRegularExpressionMatch match = i.next(); if (match.hasMatch()) { qDebug().noquote() <<"Value"<< match.captured(0);}