stringqtqregexp

Select first string using QRegularExpression


How do I get the first string ("firstStr" for the examples below) using QRegularExpression

QString readCmdOutput = "";
/* readCmdOutput = "firstStr\secondStr"; or 
readCmdOutput = "firstStr
secondStr
"
*/
readCmdOutput = QString::fromLocal8Bit(myProcess->readAllStandardOutput());  

QRegularExpression re("REGEXPRESSION");
QRegularExpressionMatch match = re.match(readCmdOutput);
if (match.hasMatch()) { 
   QString matched2 = match2.captured(0);  // has to contain "firstStr"
}

Solution

  • The correct regular expression is:

    QRegularExpression re("[^\\n\\\\]*");
    

    This regular expression matches every sequence of characters not containing a line break (\n) or a backslash (\). Note that you have to escape all backslashes. See QRegularExpression for more details.