Qt QRegExp在调试结果中获取重复
我的代码是:Qt QRegExp在调试结果中获取重复,qt,qregexp,qregularexpression,Qt,Qregexp,Qregularexpression,我的代码是: QString strExp="Sum(2+3)-Sum(5+3)"; QRegExp regexp("(Sum\\([^)]*\\))"); regexp.indexIn(strExp); QStringList lst=regexp.capturedTexts(); qDebug()<<"CapturedCounts:"<<regexp.captureCount(); qDebug()<<lst; 为什么?列表的第一个元素是整个匹配的
QString strExp="Sum(2+3)-Sum(5+3)";
QRegExp regexp("(Sum\\([^)]*\\))");
regexp.indexIn(strExp);
QStringList lst=regexp.capturedTexts();
qDebug()<<"CapturedCounts:"<<regexp.captureCount();
qDebug()<<lst;
为什么?列表的第一个元素是整个匹配的字符串 说:
QStringList QRegExp::capturedTexts()const
返回捕获的文本字符串的列表
列表中的第一个字符串是整个匹配的字符串。每个
后续列表元素包含与匹配的字符串(捕获)
regexp的子表达式
另一个例子:
QString s = "abcd123";
QRegExp re("(ab).*(12)");
qDebug() << "indexIn:" << re.indexIn(s);
qDebug() << "captureCount:" << re.captureCount();
qDebug() << "capturedTexts:" << re.capturedTexts();
如果要获取所有匹配项,可以使用以下选项:
QString strExp="Sum(2+3)-Sum(5+3)";
QRegExp regexp("(Sum\\([^)]*\\))");
regexp.indexIn(strExp);
QStringList list;
int pos = 0;
while ((pos = regexp.indexIn(strExp, pos)) != -1) {
list << regexp.cap(1);
pos += regexp.matchedLength();
}
qDebug() << "all matches:" << list;
在我的例子中,如何在Qt*中提取那种类型的表达式我不理解你的问题;QRegExp regexp(“(Sum\([^)]*\)”)很抱歉误解您的问题。我需要知道解析算术表达式的qt类,比如a*(b+c)和sum(a+b)
indexIn: 0
captureCount: 2
capturedTexts: ("abcd12", "ab", "12")
QString strExp="Sum(2+3)-Sum(5+3)";
QRegExp regexp("(Sum\\([^)]*\\))");
regexp.indexIn(strExp);
QStringList list;
int pos = 0;
while ((pos = regexp.indexIn(strExp, pos)) != -1) {
list << regexp.cap(1);
pos += regexp.matchedLength();
}
qDebug() << "all matches:" << list;
all matches: ("Sum(2+3)", "Sum(5+3)")