【问题标题】:Qt - Split and use string from lineEdit as regExpQt - 拆分并使用 lineEdit 中的字符串作为 regExp
【发布时间】:2025-12-07 02:55:02
【问题描述】:

我想通过输入一个字符串来过滤我的所有数据,听起来很简单。 这是我到目前为止得到的:

stringToSearch.replace( QRegExp(" "), "|" );

QRegExp regExp(stringToSearch,Qt::CaseInsensitive, QRegExp::Wildcard); 

model->removeRows(0,model->rowCount());
for(int row = 0; row < stringsInTable.filter(regExp).count(); row++)
{
    model->appendRow(new QStandardItem(QString(stringsInTable.filter(regExp).at(row))));
}

如果我只搜索一个单词,或者如果我在单词之间搜索“*”(如果它们以正确的顺序出现),这很好用。 但是如何搜索多个单词并且单词的顺序无关紧要?

【问题讨论】:

    标签: qt qstring qtcore qregexp


    【解决方案1】:

    您需要使用Positive Lookahead 功能并使用输入的所有单词构建您的正则表达式字符串。这是一个简单的例子(假设输入one two three):

    QRegExp re("^(?=.*one)(?=.*two)(?=.*three).*$");
    qDebug() << re.exactMatch("two three one four"); // returns true
    

    【讨论】: