QRegularExpressionMatch Class
QRegularExpressionMatch 类提供了将QRegularExpression 与字符串进行匹配的结果。更多内容...
| 头文件: | #include <QRegularExpressionMatch> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS Core) target_link_libraries(mytarget PRIVATE Qt6::Core) |
| qmake: | QT += core |
- 所有成员的列表,包括继承的成员
- QRegularExpressionMatch 属于“隐式共享类”和“字符串数据类”。
注意:该类中的所有函数均为可重入函数。
公共函数
| 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) |
详细说明
可以通过调用QRegularExpression::match()函数,或作为QRegularExpressionMatchIterator 的全局匹配的单个结果来获取QRegularExpressionMatch对象。
可以通过调用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 XML 正则表达式类的更多信息,请参阅QRegularExpression 文档。
另请参阅 QRegularExpression 。
成员函数文档
QRegularExpressionMatch::QRegularExpressionMatch()
构建一个有效的、空的 QRegularExpressionMatch 对象。正则表达式设置为默认构造的表达式;匹配类型设置为 `QRegularExpression::NoMatch `,匹配选项设置为 `QRegularExpression::NoMatchOption`。
该对象将通过成员函数hasMatch() 和hasPartialMatch() 报告未匹配。
QRegularExpressionMatch::QRegularExpressionMatch(const QRegularExpressionMatch &match)
通过复制给定的match 的结果来构建一个匹配结果。
另请参阅 operator=()。
[constexpr noexcept default, 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 。
注意:在 Qt 6.8 之前的版本中,此函数接受QString 或QStringView 作为参数,而非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。
注意:在 Qt 6.8 之前的版本中,此函数接受QString 或QStringView ,而非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。
注意:在 Qt 6.8 之前的版本中,此函数接受QString 或QStringView 作为参数,而不是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。
注意:在 Qt 6.8 之前的版本中,此函数接受QString 或QStringView 作为参数,而非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 。
注意:在 Qt 6.8 之前的版本中,此函数接受QString 或QStringView 作为参数,而非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 的捕获组,也返回false)。
注意: 即使正则表达式匹配成功,其中某些 捕获组也可能未捕获任何内容。例如,当模式中使用了条件运算符时,就可能出现这种情况:
QRegularExpression re("([a-z]+)|([A-Z]+)");
QRegularExpressionMatch m=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.8 之前的版本中,该函数的参数为 `QString ` 或 `QStringView`,而非 `QAnyStringView`。
该函数在 Qt 6.3 中引入。
[since 6.3] bool QRegularExpressionMatch::hasCaptured(int nth) const
如果nth 捕获组在主题字符串中捕获了内容,则返回true;否则(或不存在该捕获组)返回false。
注意: 隐式捕获组编号为 0,用于捕获与整个模式匹配的子字符串。
注意: 即使正则表达式匹配成功,其中某些 捕获组也可能未捕获任何内容。例如,当模式中使用了条件运算符时,就可能出现这种情况:
QRegularExpression re("([a-z]+)|([A-Z]+)");
QRegularExpressionMatch m=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
返回其 match() 函数返回了此对象的QRegularExpression 对象。
另请参阅 QRegularExpression::match()、matchType() 和matchOptions()。
[noexcept] void QRegularExpressionMatch::swap(QRegularExpressionMatch &other)
将本场比赛的结果与other 互换。此操作速度极快,且绝不会失败。
[noexcept] QRegularExpressionMatch &QRegularExpressionMatch::operator=(QRegularExpressionMatch &&match)
将匹配结果 `match ` 通过移动赋值操作赋值给该对象,并返回该结果的引用。
请注意,被移动的QRegularExpressionMatch 只能被销毁或被赋值。调用除析构函数或赋值运算符以外的其他函数,其效果未定义。
QRegularExpressionMatch &QRegularExpressionMatch::operator=(const QRegularExpressionMatch &match)
将匹配结果match 赋值给该对象,并返回该副本的引用。
相关的非成员
QDebug operator<<(QDebug debug, const QRegularExpressionMatch &match)
将匹配对象 `match ` 写入调试对象 `debug `,以便进行调试。
另请参阅 “调试技巧”。
© 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.