QRegularExpressionMatchIterator 類

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 )
(從 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 )

詳細描述

A QRegularExpressionMatchIterator object is a forward only Java-like iterator; it can be obtained by calling the QRegularExpression::globalMatch () function. A new QRegularExpressionMatchIterator will be positioned before the first result. You can then call the hasNext () function to check if there are more results available; if so, the next () function will return the next result and advance the iterator.

每個結果是 QRegularExpressionMatch 對象,保持結果的所有信息 (包括捕獲子字符串)。

例如:

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

Moreover, QRegularExpressionMatchIterator offers a peekNext () function to get the next result without advancing the iterator.

Starting with Qt 6.0, it is also possible to simply use the result of QRegularExpression::globalMatch in a range-based for loop, for instance like this:

// 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)) {
    // ...
}
					

可以檢索 QRegularExpression object the subject string was matched against by calling the regularExpression () function; the match type and the match options are available as well by calling the matchType () 和 matchOptions () 分彆。

請參考 QRegularExpression 文檔編製,瞭解有關 Qt 正則錶達式類的更多信息。

另請參閱 QRegularExpression and QRegularExpressionMatch .

成員函數文檔編製

QRegularExpressionMatchIterator:: QRegularExpressionMatchIterator ()

Constructs an empty, valid QRegularExpressionMatchIterator object. The regular expression is set to a default-constructed one; the match type to QRegularExpression::NoMatch and the match options to QRegularExpression::NoMatchOption .

援引 hasNext () member function on the constructed object will return false, as the iterator is not iterating on a valid sequence of matches.

QRegularExpressionMatchIterator:: QRegularExpressionMatchIterator (const QRegularExpressionMatchIterator & iterator )

構造 QRegularExpressionMatchIterator 對象作為副本為 iterator .

另請參閱 operator= ().

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

Constructs a QRegularExpressionMatchIterator object by moving from iterator .

Note that a moved-from QRegularExpressionMatchIterator can only be destroyed or assigned to. The effect of calling other functions than the destructor or one of the assignment operators is undefined.

該函數在 Qt 6.1 引入。

另請參閱 operator= ().

[noexcept] QRegularExpressionMatchIterator:: ~QRegularExpressionMatchIterator ()

銷毀 QRegularExpressionMatchIterator 對象。

bool QRegularExpressionMatchIterator:: hasNext () const

返迴 true if there is at least one match result ahead of the iterator; otherwise it returns false .

另請參閱 next ().

bool QRegularExpressionMatchIterator:: isValid () const

返迴 true if the iterator object was obtained as a result from the QRegularExpression::globalMatch () function invoked on a valid QRegularExpression 對象;返迴 false QRegularExpression was invalid.

另請參閱 QRegularExpression::globalMatch () 和 QRegularExpression::isValid ().

QRegularExpression::MatchOptions QRegularExpressionMatchIterator:: matchOptions () const

Returns the match options that were used to get this QRegularExpressionMatchIterator object, that is, the match options that were passed to QRegularExpression::globalMatch ().

另請參閱 QRegularExpression::globalMatch (), regularExpression (),和 matchType ().

QRegularExpression::MatchType QRegularExpressionMatchIterator:: matchType () const

Returns the match type that was used to get this QRegularExpressionMatchIterator object, that is, the match type that was passed to QRegularExpression::globalMatch ().

另請參閱 QRegularExpression::globalMatch (), regularExpression (),和 matchOptions ().

QRegularExpressionMatch QRegularExpressionMatchIterator:: next ()

Returns the next match result and advances the iterator by one position.

注意: Calling this function when the iterator is at the end of the result set leads to undefined results.

QRegularExpressionMatch QRegularExpressionMatchIterator:: peekNext () const

Returns the next match result without moving the iterator.

注意: Calling this function when the iterator is at the end of the result set leads to undefined results.

QRegularExpression QRegularExpressionMatchIterator:: regularExpression () const

返迴 QRegularExpression 對象,globalMatch() 函數會返迴此對象。

另請參閱 QRegularExpression::globalMatch (), matchType (),和 matchOptions ().

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

Swaps this iterator with other 。此操作很快且從不失敗。

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

Move-assigns the iterator to this object, and returns a reference to the result.

Note that a moved-from QRegularExpressionMatchIterator can only be destroyed or assigned to. The effect of calling other functions than the destructor or one of the assignment operators is undefined.

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

賦值迭代器 iterator to this object, and returns a reference to the copy.