本页内容

QRegularExpressionMatchIterator Class

QRegularExpressionMatchIterator 类提供了一个迭代器,用于遍历将QRegularExpression 对象与字符串进行全局匹配所得的结果。更多内容...

头文件: #include <QRegularExpressionMatchIterator>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core

注意:该类中的所有函数均为可重入的。

公共函数

QRegularExpressionMatchIterator()
QRegularExpressionMatchIterator(const QRegularExpressionMatchIterator &iterator)
(since 6.1) QRegularExpressionMatchIterator(QRegularExpressionMatchIterator &&iterator)
~QRegularExpressionMatchIterator()
bool hasNext() const
bool isValid() const
QRegularExpression::MatchOptions matchOptions() const
QRegularExpression::MatchType matchType() const
QRegularExpressionMatch next()
QRegularExpressionMatch peekNext() const
QRegularExpression regularExpression() const
void swap(QRegularExpressionMatchIterator &other)
QRegularExpressionMatchIterator &operator=(QRegularExpressionMatchIterator &&iterator)
QRegularExpressionMatchIterator &operator=(const QRegularExpressionMatchIterator &iterator)

详细说明

QRegularExpressionMatchIterator 对象是一个仅向前迭代的类似 Java 的迭代器;可通过调用QRegularExpression::globalMatch() 函数获得。一个新的 QRegularExpressionMatchIterator 将定位在第一个结果之前。 然后,您可以调用hasNext()函数来检查是否还有其他结果;如果有,next()函数将返回下一个结果并使迭代器前进。

每个结果都是一个QRegularExpressionMatch 对象,其中包含该结果的所有信息(包括捕获的子字符串)。

例如:

// extracts the words
QRegularExpression re("(\\w+)");
QString subject("the quick fox");
QRegularExpressionMatchIterator i = re.globalMatch(subject);
while (i.hasNext()) {
    QRegularExpressionMatch match = i.next();
    // ...
}

此外,QRegularExpressionMatchIterator 还提供了peekNext() 函数,可在不移动迭代器的情况下获取下一个结果。

从 Qt 6.0 开始,还可以直接在基于范围的 for 循环中使用 `QRegularExpression::globalMatch ` 的返回结果,例如如下所示:

// using a raw string literal, R"(raw_characters)", to be able to use "\w"
// without having to escape the backslash as "\\w"
QRegularExpression re(R"(\w+)");
QString subject("the quick fox");
for (const QRegularExpressionMatch &match : re.globalMatch(subject)) {
    // ...
}

您可以通过调用regularExpression()函数获取与目标字符串匹配的QRegularExpression 对象;匹配类型和匹配选项也可分别通过调用matchType()和matchOptions()获取。

有关 Qt 正则表达式类的更多信息,请参阅QRegularExpression 文档。

另请参阅 QRegularExpression 和QRegularExpressionMatch 。

成员函数文档

QRegularExpressionMatchIterator::QRegularExpressionMatchIterator()

构建一个空的、有效的 QRegularExpressionMatchIterator 对象。正则表达式设置为默认构造的表达式;匹配类型设置为QRegularExpression::NoMatch ,匹配选项设置为QRegularExpression::NoMatchOption 。

在构建的对象上调用hasNext() 成员函数将返回 false,因为该迭代器并非在遍历有效的匹配序列。

QRegularExpressionMatchIterator::QRegularExpressionMatchIterator(const QRegularExpressionMatchIterator &iterator)

构建一个 QRegularExpressionMatchIterator 对象,作为iterator 的副本。

另请参阅 operator=()。

[constexpr noexcept default, since 6.1] QRegularExpressionMatchIterator::QRegularExpressionMatchIterator(QRegularExpressionMatchIterator &&iterator)

通过从iterator 移动数据来构建一个QRegularExpressionMatchIterator对象。

请注意,被移出的 QRegularExpressionMatchIterator 只能被销毁或进行赋值。调用除析构函数或赋值运算符之外的其他函数将产生未定义的行为。

该函数在 Qt 6.1 中引入。

另请参阅 operator=()。

[noexcept] QRegularExpressionMatchIterator::~QRegularExpressionMatchIterator()

销毁QRegularExpressionMatchIterator 对象。

bool QRegularExpressionMatchIterator::hasNext() const

如果迭代器前方至少有一个匹配结果,则返回true ;否则返回false 。

另请参阅 next()。

bool QRegularExpressionMatchIterator::isValid() const

如果迭代器对象是通过在有效的QRegularExpression 对象上调用QRegularExpression::globalMatch()函数获得的,则返回true ;如果QRegularExpression 无效,则返回false 。

另请参阅 QRegularExpression::globalMatch() 和QRegularExpression::isValid()。

QRegularExpression::MatchOptions QRegularExpressionMatchIterator::matchOptions() const

返回用于获取此QRegularExpressionMatchIterator 对象的匹配选项,即传递给QRegularExpression::globalMatch() 的匹配选项。

另请参阅 QRegularExpression::globalMatch()、regularExpression() 和matchType()。

QRegularExpression::MatchType QRegularExpressionMatchIterator::matchType() const

返回用于获取此QRegularExpressionMatchIterator 对象的匹配类型,即传递给QRegularExpression::globalMatch() 的匹配类型。

另请参阅 QRegularExpression::globalMatch()、regularExpression() 和matchOptions()。

QRegularExpressionMatch QRegularExpressionMatchIterator::next()

返回下一个匹配结果,并将迭代器向前移动一个位置。

注意: 当迭代器位于结果集末尾时调用 此函数,将导致未定义的结果。

QRegularExpressionMatch QRegularExpressionMatchIterator::peekNext() const

返回下一个匹配结果,同时不移动迭代器。

注意: 当迭代器位于结果集末尾时调用 此函数,将导致未定义的结果。

QRegularExpression QRegularExpressionMatchIterator::regularExpression() const

返回该QRegularExpression 对象,其globalMatch()函数返回了此对象。

另请参阅 QRegularExpression::globalMatch()、matchType() 和matchOptions()。

[noexcept] void QRegularExpressionMatchIterator::swap(QRegularExpressionMatchIterator &other)

将该迭代器与other 互换。此操作速度极快,且绝不会失败。

[noexcept] QRegularExpressionMatchIterator &QRegularExpressionMatchIterator::operator=(QRegularExpressionMatchIterator &&iterator)

将iterator 通过移动赋值给该对象,并返回结果的引用。

请注意,被移出的QRegularExpressionMatchIterator 只能被销毁或被赋值。调用除析构函数或赋值运算符之外的其他函数,其效果未定义。

QRegularExpressionMatchIterator &QRegularExpressionMatchIterator::operator=(const QRegularExpressionMatchIterator &iterator)

将迭代器iterator 分配给该对象,并返回该副本的引用。

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.