このページでは

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)

iterator のコピーとして、QRegularExpressionMatchIterator オブジェクトを生成します。

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

イテレータの先頭に少なくとも1つの一致結果がある場合は `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()

次の一致結果を返し、イテレータを1つ進めます。

注: イテレータが結果セットの末尾にあるときにこの関数を呼び出すと 、予期しない結果が生じる可能性があります。

QRegularExpressionMatch QRegularExpressionMatchIterator::peekNext() const

イテレータを移動させることなく、次の一致結果を返します。

注: イテレータが結果セットの末尾にある状態でこの関数を呼び出すと 、予期しない結果が生じる可能性があります。

QRegularExpression QRegularExpressionMatchIterator::regularExpression() const

globalMatch() 関数がこのオブジェクトを返したQRegularExpression オブジェクトを返します。

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.