QRegularExpressionMatch Class

QRegularExpressionMatch 类提供QRegularExpression 与字符串匹配的结果。更多

Header: #include <QRegularExpressionMatch>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core

注意:该类中的所有函数都是可重入的

公共函数

QRegularExpressionMatch()
QRegularExpressionMatch(const QRegularExpressionMatch &match)
(since 6.1) QRegularExpressionMatch(QRegularExpressionMatch &&match)
~QRegularExpressionMatch()
QString captured(QAnyStringView name) const
QString captured(int nth = 0) const
qsizetype capturedEnd(QAnyStringView name) const
qsizetype capturedEnd(int nth = 0) const
qsizetype capturedLength(QAnyStringView name) const
qsizetype capturedLength(int nth = 0) const
qsizetype capturedStart(QAnyStringView name) const
qsizetype capturedStart(int nth = 0) const
QStringList capturedTexts() const
QStringView capturedView(QAnyStringView name) const
QStringView capturedView(int nth = 0) const
(since 6.3) bool hasCaptured(QAnyStringView name) const
(since 6.3) bool hasCaptured(int nth) const
bool hasMatch() const
bool hasPartialMatch() const
bool isValid() const
int lastCapturedIndex() const
QRegularExpression::MatchOptions matchOptions() const
QRegularExpression::MatchType matchType() const
QRegularExpression regularExpression() const
void swap(QRegularExpressionMatch &other)
QRegularExpressionMatch &operator=(QRegularExpressionMatch &&match)
QRegularExpressionMatch &operator=(const QRegularExpressionMatch &match)
QDebug operator<<(QDebug debug, const QRegularExpressionMatch &match)

详细描述

QRegularExpressionMatch 对象可通过调用QRegularExpression::match() 函数获得,也可作为QRegularExpressionMatchIterator 全局匹配的单个结果获得。

可以通过调用hasMatch() 函数检查匹配尝试的成功或失败。QRegularExpressionMatch 还会通过hasPartialMatch() 函数报告部分匹配成功的情况。

此外,QRegularExpressionMatch 还会返回模式字符串中捕获组捕获的子字符串。索引为 0 的隐式捕获组捕获整个匹配的结果。captured() 函数根据捕获组的索引或名称返回捕获的每个子串:

QRegularExpression re("(\\d\\d) (?<name>\\w+)");
QRegularExpressionMatch match = re.match("23 Jordan");
if (match.hasMatch()) {
    QString number = match.captured(1); // first == "23"
    QString name = match.captured("name"); // name == "Jordan"
}

对于每个捕获的子字符串,可以通过调用capturedStart() 和capturedEnd() 函数分别查询其在主题字符串中的起始和终止偏移量。使用capturedLength() 函数可以获得每个捕获子串的长度。

便捷函数capturedTexts() 将按照捕获组捕获的顺序一次性返回所有捕获的子字符串(包括与整个模式匹配的子字符串),即captured(i) == capturedTexts().at(i)

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

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

另请参阅 QRegularExpression

成员函数文档

QRegularExpressionMatch::QRegularExpressionMatch()

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

该对象将通过hasMatch() 和hasPartialMatch() 成员函数报告没有匹配。

QRegularExpressionMatch::QRegularExpressionMatch(const QRegularExpressionMatch &match)

通过复制给定match 的结果来构造匹配结果。

另请参阅 operator=() 。

[noexcept, since 6.1] QRegularExpressionMatch::QRegularExpressionMatch(QRegularExpressionMatch &&match)

通过从给定的match 中移动结果来构造匹配结果。

请注意,移动后的 QRegularExpressionMatch 只能被销毁或赋值。调用析构函数或赋值操作符之外的其他函数的效果是未定义的。

此函数在 Qt 6.1 中引入。

另请参阅 operator=()。

[noexcept] QRegularExpressionMatch::~QRegularExpressionMatch()

破坏比赛结果。

QString QRegularExpressionMatch::captured(QAnyStringView name) const

返回名为name 的捕获组捕获的子字符串。

如果名为name 的捕获组没有捕获字符串,或者没有名为name 的捕获组,则返回空值QString

注意: 在 6.8 之前的 Qt XML 版本中,该函数使用QStringQStringView ,而不是QAnyStringView

另请参阅 capturedView(),capturedStart(),capturedEnd(),capturedLength() 和QString::isNull().

QString QRegularExpressionMatch::captured(int nth = 0) const

返回nth 捕捉组捕获的子字符串。

如果nth 捕捉组没有捕获字符串,或没有捕获组,则返回空QString

注: 隐式捕获组编号 0 会捕获与整个模式匹配的子字符串。

另请参阅 capturedView()、lastCapturedIndex()、capturedStart()、capturedEnd()、capturedLength() 和QString::isNull() 。

qsizetype QRegularExpressionMatch::capturedEnd(QAnyStringView name) const

返回主题字符串中紧随名为name 的捕获组捕获的子字符串结束位置之后的偏移量。如果名为name 的捕获组未捕获字符串或不存在,则返回 -1。

注意: 在 6.8 之前的 Qt XML 版本中,此函数使用QStringQStringView ,而不是QAnyStringView

另请参阅 capturedStart()、capturedLength() 和captured()。

qsizetype QRegularExpressionMatch::capturedEnd(int nth = 0) const

返回nth 捕捉组捕获的子字符串结束位置后紧接主题字符串内部的偏移量。如果nth 捕捉组没有捕获字符串或不存在,则返回-1。

另请参阅 capturedStart()、capturedLength() 和captured()。

qsizetype QRegularExpressionMatch::capturedLength(QAnyStringView name) const

返回名为name 的捕获组捕获的子字符串的长度。

注: 如果名为name 的捕获组未捕获字符串或不存在,则此函数返回 0。

注: 在 6.8 之前的 Qt XML 版本中,此函数使用QStringQStringView ,而不是QAnyStringView

另请参阅 capturedStart()、capturedEnd() 和captured()。

qsizetype QRegularExpressionMatch::capturedLength(int nth = 0) const

返回nth 捕捉组捕获的子字符串的长度。

注: 如果nth 捕捉组未捕获字符串或不存在,则此函数返回 0。

另请参阅 capturedStart()、capturedEnd() 和captured()。

qsizetype QRegularExpressionMatch::capturedStart(QAnyStringView name) const

返回主题字符串中与名为name 的捕获组捕获的子字符串起始位置相对应的偏移量。如果名为name 的捕获组未捕获字符串或不存在,则返回 -1。

注意: 在 6.8 之前的 Qt XML 版本中,此函数使用QStringQStringView ,而不是QAnyStringView

另请参阅 capturedEnd()、capturedLength() 和captured()。

qsizetype QRegularExpressionMatch::capturedStart(int nth = 0) const

返回主题字符串中与nth 捕捉组捕获的子字符串起始位置相对应的偏移量。如果nth 捕捉组没有捕获字符串或不存在,则返回-1。

另请参阅 capturedEnd()、capturedLength() 和captured()。

QStringList QRegularExpressionMatch::capturedTexts() const

返回捕获组捕获的所有字符串的列表,按捕获组在模式字符串中出现的顺序排列。该列表包括隐式捕获组 0,捕获与整个模式匹配的子字符串。

QStringView QRegularExpressionMatch::capturedView(QAnyStringView name) const

返回名为name 的捕获组捕获的字符串的视图。

如果名为name 的捕获组没有捕获字符串,或者没有名为name 的捕获组,则返回空值QStringView

注意: 在 6.8 之前的 Qt XML 版本中,该函数使用QStringQStringView ,而不是QAnyStringView

另请参阅 captured(),capturedStart(),capturedEnd(),capturedLength() 和QStringView::isNull().

QStringView QRegularExpressionMatch::capturedView(int nth = 0) const

返回nth 捕捉组捕获的子字符串的视图。

如果nth 捕捉组没有捕获字符串,或没有捕获组,则返回空QStringView

注: 隐式捕获组编号 0 会捕获与整个模式匹配的子字符串。

另请参阅 captured()、lastCapturedIndex()、capturedStart()、capturedEnd()、capturedLength() 和QStringView::isNull() 。

[since 6.3] bool QRegularExpressionMatch::hasCaptured(QAnyStringView name) const

如果名为name 的捕获组捕获了主题字符串中的内容,则返回 true,否则返回 false(或者如果没有名为name 的捕获组)。

注意: 即使正则表达式匹配,正则表达式中的某些捕获组也可能没有捕获任何内容。例如,如果在正则表达式中使用了条件操作符,就可能出现这种情况:

QRegularExpressionre("([a-z]+)|([A-Z]+)");QRegularExpressionMatchm=re.match("UPPERCASE");if(m.hasMatch()) {    qDebug() << m.hasCaptured(0); // true
    qDebug() << m.hasCaptured(1); // false
    qDebug() << m.hasCaptured(2); // true
}

同样,捕获组可以捕获长度为 0 的子串;对于这样的捕获组,该函数将返回true

注意: 在 6.8 之前的 Qt XML 版本中,此函数使用QStringQStringView ,而不是QAnyStringView

此函数在 Qt 6.3 中引入。

另请参阅 captured() 和hasMatch()。

[since 6.3] bool QRegularExpressionMatch::hasCaptured(int nth) const

如果nth 捕捉组捕捉到了主题字符串中的内容,则返回 true,否则返回 false(或者没有捕捉组)。

注意: 隐式捕获组编号 0 会捕获与整个模式匹配的子字符串。

注意: 即使正则表达式匹配,正则表达式中的某些捕获组可能也不会捕获任何内容。例如,如果在模式中使用了条件操作符,就可能出现这种情况:

QRegularExpressionre("([a-z]+)|([A-Z]+)");QRegularExpressionMatchm=re.match("UPPERCASE");if(m.hasMatch()) {    qDebug() << m.hasCaptured(0); // true
    qDebug() << m.hasCaptured(1); // false
    qDebug() << m.hasCaptured(2); // true
}

同样,捕获组可以捕获长度为 0 的子串;对于这样的捕获组,该函数将返回true

此函数在 Qt 6.3 中引入。

另请参阅 captured()、lastCapturedIndex() 和hasMatch()。

bool QRegularExpressionMatch::hasMatch() const

如果正则表达式与主题字符串匹配,则返回true ,否则返回 false。

另请参阅 QRegularExpression::match() 和hasPartialMatch()。

bool QRegularExpressionMatch::hasPartialMatch() const

如果正则表达式与主题字符串部分匹配,则返回true ,否则返回 false。

注意: 只有明确使用部分匹配类型之一的匹配才能产生部分匹配。不过,如果匹配完全成功,该函数将返回 false,而hasMatch() 将返回 true。

另请参阅 QRegularExpression::match(),QRegularExpression::MatchType, 和hasMatch().

bool QRegularExpressionMatch::isValid() const

如果匹配对象是在有效的QRegularExpression 对象上调用QRegularExpression::match() 函数的结果,则返回true ;如果QRegularExpression 无效,则返回false

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

int QRegularExpressionMatch::lastCapturedIndex() const

返回最后捕获的捕获分组的索引,包括隐式捕获分组 0。 这可用于提取所有被捕获的子字符串:

QRegularExpressionMatch match = re.match(string);
for (int i = 0; i <= match.lastCapturedIndex(); ++i) {
    QString captured = match.captured(i);
    // ...
}

请注意,一些索引小于 lastCapturedIndex() 的捕获组可能没有匹配,因此捕获不到任何内容。

如果正则表达式不匹配,该函数将返回-1。

另请参见 hasCaptured()、captured()、capturedStart()、capturedEnd() 和capturedLength()。

QRegularExpression::MatchOptions QRegularExpressionMatch::matchOptions() const

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

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

QRegularExpression::MatchType QRegularExpressionMatch::matchType() const

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

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

QRegularExpression QRegularExpressionMatch::regularExpression() const

返回匹配()函数返回此对象的QRegularExpression 对象。

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

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

将匹配结果与other 互换。该操作速度非常快,从未出现过故障。

[noexcept] QRegularExpressionMatch &QRegularExpressionMatch::operator=(QRegularExpressionMatch &&match)

Move 将匹配结果match 赋值给该对象,并返回该结果的引用。

请注意,moved-fromQRegularExpressionMatch 只能被销毁或赋值。调用析构函数或赋值操作符之外的其他函数的效果是未定义的。

QRegularExpressionMatch &QRegularExpressionMatch::operator=(const QRegularExpressionMatch &match)

将匹配结果match 赋值给此对象,并返回对副本的引用。

相关非成员

QDebug operator<<(QDebug debug, const QRegularExpressionMatch &match)

将匹配对象match 写入调试对象debug 供调试使用。

另请参阅 调试技巧

© 2025 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.