QRegularExpressionMatchIterator Class▲
-
Header: QRegularExpressionMatchIterator
-
Since: Qt 5.0
-
CMake:
find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
-
qmake: QT += core
-
Group: QRegularExpressionMatchIterator is part of tools, Implicitly Shared Classes
Detailed Description▲
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.
Each result is a QRegularExpressionMatch object holding all the information for that result (including captured substrings).
For instance:
// 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 &
amp;match : re.globalMatch(subject)) {
// ...
}
You can retrieve the 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() and the matchOptions() respectively.
Please refer to the QRegularExpression documentation for more information about the Qt regular expression classes.
See Also▲
See also QRegularExpression, QRegularExpressionMatch
Member Function Documentation▲
[since 5.1] 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.
Invoking the hasNext() member function on the constructed object will return false, as the iterator is not iterating on a valid sequence of matches.
This function was introduced in Qt 5.1.
QRegularExpressionMatchIterator::QRegularExpressionMatchIterator(const QRegularExpressionMatchIterator &iterator)▲
Constructs a QRegularExpressionMatchIterator object as a copy of iterator.
See Also▲
See also operator=()
[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.
This function was introduced in Qt 6.1.
See Also▲
See also operator=()
QRegularExpressionMatchIterator::~QRegularExpressionMatchIterator()▲
Destroys the QRegularExpressionMatchIterator object.
bool QRegularExpressionMatchIterator::hasNext() const▲
Returns true if there is at least one match result ahead of the iterator; otherwise it returns false.
See Also▲
See also next()
bool QRegularExpressionMatchIterator::isValid() const▲
Returns true if the iterator object was obtained as a result from the QRegularExpression::globalMatch() function invoked on a valid QRegularExpression object; returns false if the QRegularExpression was invalid.
See Also▲
See also 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().
See Also▲
See also 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().
See Also▲
See also 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▲
Returns the QRegularExpression object whose globalMatch() function returned this object.
See Also▲
See also QRegularExpression::globalMatch(), matchType(), matchOptions()
void QRegularExpressionMatchIterator::swap(QRegularExpressionMatchIterator &other)▲
Swaps the iterator other with this iterator object. This operation is very fast and never fails.
QRegularExpressionMatchIterator &QRegularExpressionMatchIterator::operator=(const QRegularExpressionMatchIterator &iterator)▲
Assigns the iterator iterator to this object, and returns a reference to the copy.
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.